<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><div id="myscoll"></div><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>MySQL基本使用 | XiaoFei🥝</title><meta name="keywords" content="MySQL基本使用"><meta name="author" content="XiaoFei🥝"><meta name="copyright" content="XiaoFei🥝"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="MySQL基本使用">
<meta property="og:type" content="article">
<meta property="og:title" content="MySQL基本使用">
<meta property="og:url" content="https://www.naste.top:1024/posts/75c6e52f.html">
<meta property="og:site_name" content="XiaoFei🥝">
<meta property="og:description" content="MySQL基本使用">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://www.naste.top:9000/webp/1515952743786.webp">
<meta property="article:published_time" content="2022-07-03T10:01:02.000Z">
<meta property="article:modified_time" content="2024-04-26T09:52:18.766Z">
<meta property="article:author" content="XiaoFei🥝">
<meta property="article:tag" content="MySQL">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://www.naste.top:9000/webp/1515952743786.webp"><link rel="shortcut icon" href="/"><link rel="canonical" href="https://www.naste.top:1024/posts/75c6e52f"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/6.0.0/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.staticfile.org/fancyapps-ui/4.0.31/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.json","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: {"limitDay":365,"position":"top","messagePrev":"It has been","messageNext":"days since the last update, the content of the article may be outdated."},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":230},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdnjs.cloudflare.com/ajax/libs/flickr-justified-gallery/2.1.2/fjGallery.min.js',
      css: 'https://cdnjs.cloudflare.com/ajax/libs/flickr-justified-gallery/2.1.2/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'MySQL基本使用',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-04-26 17:52:18'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="https://cdn1.tianli0.top/npm/element-ui@2.15.6/packages/theme-chalk/lib/index.css"><style id="themeColor"></style><style id="rightSide"></style><style id="transPercent"></style><style id="blurNum"></style><style id="settingStyle"></style><span id="fps"></span><style id="defineBg"></style><style id="menu_shadow"></style><svg aria-hidden="true" style="position:absolute; overflow:hidden; width:0; height:0"><symbol id="icon-sun" viewBox="0 0 1024 1024"><path d="M960 512l-128 128v192h-192l-128 128-128-128H192v-192l-128-128 128-128V192h192l128-128 128 128h192v192z" fill="#FFD878" p-id="8420"></path><path d="M736 512a224 224 0 1 0-448 0 224 224 0 1 0 448 0z" fill="#FFE4A9" p-id="8421"></path><path d="M512 109.248L626.752 224H800v173.248L914.752 512 800 626.752V800h-173.248L512 914.752 397.248 800H224v-173.248L109.248 512 224 397.248V224h173.248L512 109.248M512 64l-128 128H192v192l-128 128 128 128v192h192l128 128 128-128h192v-192l128-128-128-128V192h-192l-128-128z" fill="#4D5152" p-id="8422"></path><path d="M512 320c105.888 0 192 86.112 192 192s-86.112 192-192 192-192-86.112-192-192 86.112-192 192-192m0-32a224 224 0 1 0 0 448 224 224 0 0 0 0-448z" fill="#4D5152" p-id="8423"></path></symbol><symbol id="icon-moon" viewBox="0 0 1024 1024"><path d="M611.370667 167.082667a445.013333 445.013333 0 0 1-38.4 161.834666 477.824 477.824 0 0 1-244.736 244.394667 445.141333 445.141333 0 0 1-161.109334 38.058667 85.077333 85.077333 0 0 0-65.066666 135.722666A462.08 462.08 0 1 0 747.093333 102.058667a85.077333 85.077333 0 0 0-135.722666 65.024z" fill="#FFB531" p-id="11345"></path><path d="M329.728 274.133333l35.157333-35.157333a21.333333 21.333333 0 1 0-30.165333-30.165333l-35.157333 35.157333-35.114667-35.157333a21.333333 21.333333 0 0 0-30.165333 30.165333l35.114666 35.157333-35.114666 35.157334a21.333333 21.333333 0 1 0 30.165333 30.165333l35.114667-35.157333 35.157333 35.157333a21.333333 21.333333 0 1 0 30.165333-30.165333z" fill="#030835" p-id="11346"></path></symbol></svg><!-- hexo injector head_end start --><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiperstyle.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/tag_plugins.css" media="defer" onload="this.media='all'"><script src="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/carousel-touch.js"></script><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-wowjs/lib/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-filter-gitcalendar/lib/gitcalendar.css" media="print" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="XiaoFei🥝" type="application/atom+xml">
</head><body><div id="loading-box" onclick="document.getElementById(&quot;loading-box&quot;).classList.add(&quot;loaded&quot;)"><div class="loading-bg"><div class="loading-img"></div><div class="loading-image-dot"></div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://www.naste.top:9000/images/cat.jpg" onerror="onerror=null;src='/assets/r1.jpg'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">149</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">99</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">46</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-home"></use></svg><span class="menu_word" style="font-size:17px"> 首页</span></a></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon--article"></use></svg><span class="menu_word" style="font-size:17px"> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/archives/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-guidang1">                   </use></svg><span class="menu_word" style="font-size:17px"> 归档</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-sekuaibiaoqian">                   </use></svg><span class="menu_word" style="font-size:17px"> 标签</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/categories/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-fenlei">                   </use></svg><span class="menu_word" style="font-size:17px"> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-pinweishenghuo"></use></svg><span class="menu_word" style="font-size:17px"> 休闲</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/life/music/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-yinle">                   </use></svg><span class="menu_word" style="font-size:17px"> 八音盒</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/life/movies/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-dianying1">                   </use></svg><span class="menu_word" style="font-size:17px"> 影院</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/life/games/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-youxishoubing">                   </use></svg><span class="menu_word" style="font-size:17px"> 游戏</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-xiangzi"></use></svg><span class="menu_word" style="font-size:17px"> 八宝箱</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/box/gallery/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-tubiaozhizuomoban">                   </use></svg><span class="menu_word" style="font-size:17px"> 画廊</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/box/animation/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-nvwumao">                   </use></svg><span class="menu_word" style="font-size:17px"> 动画</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/box/nav/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-zhifengche">                   </use></svg><span class="menu_word" style="font-size:17px"> 网址导航</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-shejiaoxinxi"></use></svg><span class="menu_word" style="font-size:17px"> 社交</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/social/fcircle/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-pengyouquan">                   </use></svg><span class="menu_word" style="font-size:17px"> 朋友圈</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/comments/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-liuyan">                   </use></svg><span class="menu_word" style="font-size:17px"> 留言板</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/social/link/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-lianjie">                   </use></svg><span class="menu_word" style="font-size:17px"> 友人帐</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-wangye"></use></svg><span class="menu_word" style="font-size:17px"> 网站</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/site/census/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon--tongjibiao">                   </use></svg><span class="menu_word" style="font-size:17px"> 网站统计</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/site/echarts/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-shujutongji1">                   </use></svg><span class="menu_word" style="font-size:17px"> 文章统计</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/site/time/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-xianxingshalou">                   </use></svg><span class="menu_word" style="font-size:17px"> 旧时光</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-maoliang"></use></svg><span class="menu_word" style="font-size:17px"> 个人</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/personal/bb/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-qunliaotian">                   </use></svg><span class="menu_word" style="font-size:17px"> 唠叨</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/personal/love/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-love-sign">                   </use></svg><span class="menu_word" style="font-size:17px"> 恋爱小屋</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/personal/about/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-paperplane">                   </use></svg><span class="menu_word" style="font-size:17px"> 关于</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/encryption/%E6%9C%BA%E5%9C%BA/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-paperplane">                   </use></svg><span class="menu_word" style="font-size:17px"> 机场</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">XiaoFei🥝</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-home"></use></svg><span class="menu_word" style="font-size:17px"> 首页</span></a></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon--article"></use></svg><span class="menu_word" style="font-size:17px"> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/archives/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-guidang1">                   </use></svg><span class="menu_word" style="font-size:17px"> 归档</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-sekuaibiaoqian">                   </use></svg><span class="menu_word" style="font-size:17px"> 标签</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/categories/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-fenlei">                   </use></svg><span class="menu_word" style="font-size:17px"> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-pinweishenghuo"></use></svg><span class="menu_word" style="font-size:17px"> 休闲</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/life/music/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-yinle">                   </use></svg><span class="menu_word" style="font-size:17px"> 八音盒</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/life/movies/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-dianying1">                   </use></svg><span class="menu_word" style="font-size:17px"> 影院</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/life/games/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-youxishoubing">                   </use></svg><span class="menu_word" style="font-size:17px"> 游戏</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-xiangzi"></use></svg><span class="menu_word" style="font-size:17px"> 八宝箱</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/box/gallery/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-tubiaozhizuomoban">                   </use></svg><span class="menu_word" style="font-size:17px"> 画廊</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/box/animation/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-nvwumao">                   </use></svg><span class="menu_word" style="font-size:17px"> 动画</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/box/nav/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-zhifengche">                   </use></svg><span class="menu_word" style="font-size:17px"> 网址导航</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-shejiaoxinxi"></use></svg><span class="menu_word" style="font-size:17px"> 社交</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/social/fcircle/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-pengyouquan">                   </use></svg><span class="menu_word" style="font-size:17px"> 朋友圈</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/comments/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-liuyan">                   </use></svg><span class="menu_word" style="font-size:17px"> 留言板</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/social/link/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-lianjie">                   </use></svg><span class="menu_word" style="font-size:17px"> 友人帐</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-wangye"></use></svg><span class="menu_word" style="font-size:17px"> 网站</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/site/census/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon--tongjibiao">                   </use></svg><span class="menu_word" style="font-size:17px"> 网站统计</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/site/echarts/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-shujutongji1">                   </use></svg><span class="menu_word" style="font-size:17px"> 文章统计</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/site/time/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-xianxingshalou">                   </use></svg><span class="menu_word" style="font-size:17px"> 旧时光</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-maoliang"></use></svg><span class="menu_word" style="font-size:17px"> 个人</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/personal/bb/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-qunliaotian">                   </use></svg><span class="menu_word" style="font-size:17px"> 唠叨</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/personal/love/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-love-sign">                   </use></svg><span class="menu_word" style="font-size:17px"> 恋爱小屋</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/personal/about/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-paperplane">                   </use></svg><span class="menu_word" style="font-size:17px"> 关于</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/encryption/%E6%9C%BA%E5%9C%BA/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-paperplane">                   </use></svg><span class="menu_word" style="font-size:17px"> 机场</span></a></li></ul></div></div><center id="name-container"><a id="page-name" href="javascript:scrollToTop()">PAGE_NAME</a></center><div id="nav-right"><div id="search-button"><a class="search faa-parent animated-hover" title="检索站内任何你想要的信息"><svg class="faa-tada icon" style="height:24px;width:24px;fill:currentColor;position:relative;top:6px" aria-hidden="true"><use xlink:href="#icon-valentine_-search-love-find-heart"></use></svg><span> 搜索</span></a></div><a class="meihua faa-parent animated-hover" onclick="toggleWinbox()" title="美化设置-自定义你的风格" id="meihua-button"><svg class="faa-tada icon" style="height:26px;width:26px;fill:currentColor;position:relative;top:8px" aria-hidden="true"><use xlink:href="#icon-tupian1"></use></svg></a><a class="sun_moon faa-parent animated-hover" onclick="switchNightMode()" title="浅色和深色模式转换" id="nightmode-button"><svg class="faa-tada" style="height:25px;width:25px;fill:currentColor;position:relative;top:7px" viewBox="0 0 1024 1024"><use id="modeicon" xlink:href="#icon-moon">       </use></svg></a><div id="toggle-menu"><a><i class="fas fa-bars fa-fw"></i></a></div></div></div></nav><div id="post-info"><h1 class="post-title">MySQL基本使用</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><svg class="meta_icon post-meta-icon" style="width:30px;height:30px;position:relative;top:10px"><use xlink:href="#icon-rili"></use></svg><span class="post-meta-label">发表于 </span><time class="post-meta-date-created" datetime="2022-07-03T10:01:02.000Z" title="发表于 2022-07-03 18:01:02">2022-07-03</time><span class="post-meta-separator">|</span><svg class="meta_icon post-meta-icon" style="width:18px;height:18px;position:relative;top:5px"><use xlink:href="#icon-gengxin1"></use></svg><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-04-26T09:52:18.766Z" title="更新于 2024-04-26 17:52:18">2024-04-26</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><svg class="meta_icon post-meta-icon" style="width:18px;height:18px;position:relative;top:5px"><use xlink:href="#icon-biaoqian"></use></svg><a class="post-meta-categories" href="/categories/%E4%B8%AD%E9%97%B4%E4%BB%B6/">中间件</a><i class="fas fa-angle-right post-meta-separator"></i><svg class="meta_icon post-meta-icon" style="width:18px;height:18px;position:relative;top:5px"><use xlink:href="#icon-biaoqian"></use></svg><a class="post-meta-categories" href="/categories/%E4%B8%AD%E9%97%B4%E4%BB%B6/MySQL/">MySQL</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><svg class="meta_icon post-meta-icon" style="width:25px;height:25px;position:relative;top:8px"><use xlink:href="#icon-charuword"></use></svg><span class="post-meta-label">字数总计:</span><span class="word-count">1.2w</span><span class="post-meta-separator">|</span><svg class="meta_icon post-meta-icon" style="width:20px;height:20px;position:relative;top:5px"><use xlink:href="#icon-shizhong"></use></svg><span class="post-meta-label">阅读时长:</span><span>47分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="MySQL基本使用"><svg class="meta_icon post-meta-icon" style="width:25px;height:25px;position:relative;top:5px"><use xlink:href="#icon-eye"></use></svg><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><section class="main-hero-waves-area waves-area"><svg class="waves-svg" xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto"><defs><path id="gentle-wave" d="M -160 44 c 30 0 58 -18 88 -18 s 58 18 88 18 s 58 -18 88 -18 s 58 18 88 18 v 44 h -352 Z"></path></defs><g class="parallax"><use href="#gentle-wave" x="48" y="0"></use><use href="#gentle-wave" x="48" y="3"></use><use href="#gentle-wave" x="48" y="5"></use><use href="#gentle-wave" x="48" y="7"></use></g></svg></section></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="一、连接MySQL"><a href="#一、连接MySQL" class="headerlink" title="一、连接MySQL"></a>一、连接MySQL</h1><ol>
<li><p>使用MySQL自带的连接客户端工具（只能连接root用户）</p>
</li>
<li><p>使用cmd命令行连接MySQL</p>
 <figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">模板：mysql -h 主机ip -<span class="selector-tag">P</span> 端口号 -u 用户名 -<span class="selector-tag">p</span>密码</span><br><span class="line">例：mysql -h localhost -<span class="selector-tag">P</span> <span class="number">3306</span> -u root -proot</span><br></pre></td></tr></table></figure>
</li>
<li><p>连接本机MySQL（主机和ip可以省略）：</p>
 <figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">mysql</span> 【-h localhost -P <span class="number">3306</span>】 -u root -proot</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h1 id="二、MySQL常见命令"><a href="#二、MySQL常见命令" class="headerlink" title="二、MySQL常见命令"></a>二、MySQL常见命令</h1><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> database `数据库名`;# 简单创建数据库</span><br><span class="line"><span class="keyword">show</span> databases;# 查看数据库</span><br><span class="line">use `数据库名`;# 使用数据库</span><br><span class="line"><span class="keyword">show</span> tables;# 查看指定数据库下面的所有表</span><br><span class="line"><span class="keyword">show</span> tables <span class="keyword">from</span> `数据库名`; # 查看指定数据库下面的所有表</span><br><span class="line"><span class="keyword">select</span> database(); # 查看当前处于哪个数据库中</span><br><span class="line"></span><br><span class="line"># 简单创建表</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> stu_info(</span><br><span class="line">	id <span class="type">int</span>,</span><br><span class="line">    name <span class="type">varchar</span>(<span class="number">20</span>)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"># 查看表结构</span><br><span class="line"><span class="keyword">desc</span> `表名`;</span><br><span class="line"></span><br><span class="line"># 查询数据</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> `表名`;</span><br><span class="line"></span><br><span class="line"># 添加数据</span><br><span class="line">格式：<span class="keyword">INSERT</span> <span class="keyword">INTO</span> `表名`(`字段名`) <span class="keyword">VALUES</span>(`字段值`);</span><br><span class="line">例：<span class="keyword">INSERT</span> <span class="keyword">INTO</span> stu_info(id,name) <span class="keyword">VALUES</span>(<span class="number">1</span>,<span class="string">&#x27;张三&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># 修改操作</span><br><span class="line">格式：<span class="keyword">UPDATE</span>  `表名` <span class="keyword">SET</span> `字段名` <span class="operator">=</span> <span class="string">&#x27;字段值&#x27;</span>;</span><br><span class="line">例：<span class="keyword">UPDATE</span>  stu_info <span class="keyword">SET</span> name <span class="operator">=</span> <span class="string">&#x27;李四&#x27;</span>;</span><br><span class="line"></span><br><span class="line"># 删除数据</span><br><span class="line">格式：<span class="keyword">DELETE</span> <span class="keyword">FROM</span> `表名` <span class="keyword">WHERE</span> 条件;</span><br><span class="line">例：<span class="keyword">DELETE</span> <span class="keyword">FROM</span> stu_info <span class="keyword">WHERE</span> id <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"># 查询数据库版本</span><br><span class="line"><span class="keyword">SELECT</span> version();</span><br></pre></td></tr></table></figure>
<h1 id="三、MySQL语法规范"><a href="#三、MySQL语法规范" class="headerlink" title="三、MySQL语法规范"></a>三、MySQL语法规范</h1><ul>
<li>不区分大小写，但建议关键字大写，表名，列名小写</li>
<li>每条命令最好用分号结尾</li>
<li>每条命令根据需要，可以进行缩写，或换行</li>
<li>注释：<ul>
<li>单行注释：<code># 注释文字</code></li>
<li>单行注释：<code>-- 注释文字</code></li>
<li>多行注释：<code>/* 注释文字 */</code></li>
</ul>
</li>
</ul>
<h1 id="四、DQL（数据查询语言DQL）"><a href="#四、DQL（数据查询语言DQL）" class="headerlink" title="四、DQL（数据查询语言DQL）"></a>四、DQL（数据查询语言DQL）</h1><h2 id="1、基础查询"><a href="#1、基础查询" class="headerlink" title="1、基础查询"></a>1、基础查询</h2><blockquote>
<p><strong>知识点</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"># 进阶一：基础查询</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">		语法：</span></span><br><span class="line"><span class="comment">		SELECT 查询列表 FROM 表名;</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">		类似于：</span></span><br><span class="line"><span class="comment">		System.out.println(&quot;打印东西&quot;);</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">		特点：</span></span><br><span class="line"><span class="comment">		1、查询列表可以是：表中的字段，常量值，表达式，函数</span></span><br><span class="line"><span class="comment">		2、查询的结果是一个虚拟的表格</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">		起别名</span></span><br><span class="line"><span class="comment">		1、便于理解</span></span><br><span class="line"><span class="comment">		2、如果多表查询的时候，出现字段名重复，可以使用起别名来区分</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">		+ 号的作用</span></span><br><span class="line"><span class="comment">		Java中：</span></span><br><span class="line"><span class="comment">			运算符，两个操作数都要为数值型</span></span><br><span class="line"><span class="comment">			连接符，只要有一个操作为字符串</span></span><br><span class="line"><span class="comment">		mysql中：</span></span><br><span class="line"><span class="comment">			运算符：只有一个功能，运算符</span></span><br><span class="line"><span class="comment">			连接符：使用CONCAT函数将字符串拼接</span></span><br><span class="line"><span class="comment">			</span></span><br><span class="line"><span class="comment">		IFNULL 函数</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>查询例子</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"># <span class="number">1</span>、查询表中的单个字段</span><br><span class="line"><span class="keyword">SELECT</span> last_name <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># 查询表中的多个字段</span><br><span class="line"><span class="keyword">SELECT</span> last_name,email <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># 查询表中的所有字段</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># 查询常量值</span><br><span class="line"><span class="keyword">SELECT</span> <span class="number">100</span>;</span><br><span class="line"><span class="keyword">SELECT</span> <span class="string">&#x27;1000&#x27;</span>;</span><br><span class="line"></span><br><span class="line"># 表达式查询</span><br><span class="line"><span class="keyword">SELECT</span> <span class="number">100</span><span class="operator">*</span><span class="number">199</span>;</span><br><span class="line"></span><br><span class="line"># 查询函数</span><br><span class="line"><span class="keyword">SELECT</span> VERSION();# 查询数据库版本</span><br><span class="line"></span><br><span class="line"># 方式一：使用 <span class="keyword">AS</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="number">100</span><span class="operator">*</span><span class="number">999</span> <span class="keyword">AS</span> 结果;</span><br><span class="line"></span><br><span class="line"># 方式二：使用空格</span><br><span class="line"><span class="keyword">SELECT</span> <span class="number">100</span><span class="operator">*</span><span class="number">999</span> 结果;</span><br><span class="line"></span><br><span class="line"># 字段去重：在字段前面加上 <span class="keyword">DISTINCT</span> 编号，可以使查询出来的结果中，该字段不会出现重复的值</span><br><span class="line"># 查询员工表中的涉及的所有的部门编号</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> department_id <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># 查询员工和姓连接为一个姓名，使用CONCAT函数将字符串进行拼接</span><br><span class="line"><span class="keyword">SELECT</span> CONCAT(last_name,first_name) <span class="keyword">AS</span> 姓名 <span class="keyword">FROM</span> employees;</span><br></pre></td></tr></table></figure>
<h2 id="2、条件查询"><a href="#2、条件查询" class="headerlink" title="2、条件查询"></a>2、条件查询</h2><blockquote>
<p><strong>语法</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"># 进阶<span class="number">2</span>：条件查询</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">		语法：</span></span><br><span class="line"><span class="comment">			SELECT 查询列表 FROM 表名 WHERE 筛选条件;</span></span><br><span class="line"><span class="comment">		分类：</span></span><br><span class="line"><span class="comment">			1、按条件表达式筛选</span></span><br><span class="line"><span class="comment">				条件运算符： &gt; &lt; = !=(&lt;&gt;) &gt;= &lt;=</span></span><br><span class="line"><span class="comment">			2、逻辑表达式筛选</span></span><br><span class="line"><span class="comment">				逻辑运算符：&amp;&amp; || ! （and or not）</span></span><br><span class="line"><span class="comment">			3、模糊查询</span></span><br><span class="line"><span class="comment">				LIKE</span></span><br><span class="line"><span class="comment">				BETWEEN AND</span></span><br><span class="line"><span class="comment">				IN</span></span><br><span class="line"><span class="comment">				IS NULL</span></span><br><span class="line"><span class="comment">			4、is null 和 &lt;=&gt;</span></span><br><span class="line"><span class="comment">				IS NULL：仅仅可以判断 NULL 值，可读性比较高，建议使用</span></span><br><span class="line"><span class="comment">				&lt;=&gt;：既可以判断 NULL，也可以判断普通的数值，可读性比较低</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>案例</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"># 进阶<span class="number">2</span>：条件查询</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">		语法：</span></span><br><span class="line"><span class="comment">			SELECT 查询列表 FROM 表名 WHERE 筛选条件;</span></span><br><span class="line"><span class="comment">		分类：</span></span><br><span class="line"><span class="comment">			1、按条件表达式筛选</span></span><br><span class="line"><span class="comment">				条件运算符： &gt; &lt; = !=(&lt;&gt;) &gt;= &lt;=</span></span><br><span class="line"><span class="comment">			2、逻辑表达式筛选</span></span><br><span class="line"><span class="comment">				逻辑运算符：&amp;&amp; || ! （and or not）</span></span><br><span class="line"><span class="comment">			3、模糊查询</span></span><br><span class="line"><span class="comment">				LIKE</span></span><br><span class="line"><span class="comment">				BETWEEN AND</span></span><br><span class="line"><span class="comment">				IN</span></span><br><span class="line"><span class="comment">				IS NULL</span></span><br><span class="line"><span class="comment">			4、is null 和 &lt;=&gt;</span></span><br><span class="line"><span class="comment">				IS NULL：仅仅可以判断 NULL 值，可读性比较高，建议使用</span></span><br><span class="line"><span class="comment">				&lt;=&gt;：既可以判断 NULL，也可以判断普通的数值，可读性比较低</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、按条件表达式查询</span><br><span class="line"></span><br><span class="line"># 案例：查询工资<span class="operator">&gt;</span><span class="number">12000</span>的员工信息</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> salary <span class="operator">&gt;</span> <span class="number">12000</span></span><br><span class="line"></span><br><span class="line"># 查询部门编号不等于<span class="number">90</span>号的员工名和部门编号</span><br><span class="line"><span class="keyword">SELECT</span> last_name,department_id <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> department_id<span class="operator">&lt;&gt;</span><span class="number">90</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、逻辑表达式筛选</span><br><span class="line"></span><br><span class="line"># 查询工资在<span class="number">10000</span>到<span class="number">20000</span>的员工名，工资及奖金</span><br><span class="line"><span class="keyword">SELECT</span> last_name , salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> salary <span class="operator">&gt;=</span> <span class="number">10000</span> <span class="keyword">AND</span> salary <span class="operator">&lt;=</span> <span class="number">20000</span>;</span><br><span class="line"></span><br><span class="line"># 查询工资低于<span class="number">10000</span>或大于等于<span class="number">20000</span>的员工名，工资及奖金</span><br><span class="line"><span class="keyword">SELECT</span> last_name , salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> salary <span class="operator">&lt;=</span> <span class="number">10000</span> <span class="keyword">OR</span> salary <span class="operator">&gt;=</span> <span class="number">20000</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">3</span>、模糊查询</span><br><span class="line"></span><br><span class="line"># 查询员工张名字字符包含a的员工信息</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> last_name <span class="keyword">LIKE</span> <span class="string">&#x27;%a%&#x27;</span></span><br><span class="line"></span><br><span class="line"># 查询员工名中第三个字符为n，第五个字符为l的员工名和工资</span><br><span class="line"><span class="keyword">SELECT</span> last_name,salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> last_name <span class="keyword">LIKE</span> <span class="string">&#x27;%__n_l%&#x27;</span>;</span><br><span class="line"></span><br><span class="line"># 查询员工中第二个字符为_的员工名，使用 \ 进行转义</span><br><span class="line"><span class="keyword">SELECT</span> last_name <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> last_name <span class="keyword">LIKE</span> <span class="string">&#x27;%_\_%&#x27;</span>;</span><br><span class="line"></span><br><span class="line"># 查询员工工资在<span class="number">10000</span><span class="number">-20000</span>之间的员工信息</span><br><span class="line"><span class="keyword">SELECT</span> last_name ,salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> salary <span class="keyword">BETWEEN</span> <span class="number">10000</span> <span class="keyword">AND</span> <span class="number">20000</span>;</span><br><span class="line"># 相当于</span><br><span class="line"><span class="keyword">SELECT</span> last_name ,salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> salary <span class="operator">&gt;=</span> <span class="number">10000</span> <span class="keyword">AND</span> salary <span class="operator">&lt;=</span> <span class="number">20000</span>;</span><br><span class="line"></span><br><span class="line"># <span class="keyword">in</span>：查询员工编号在属于某个集合的员工信息</span><br><span class="line"><span class="keyword">SELECT</span> last_name , employee_id <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> employee_id <span class="keyword">IN</span> (<span class="number">100</span>,<span class="number">101</span>,<span class="number">102</span>,<span class="number">103</span>,<span class="number">104</span>);</span><br><span class="line"></span><br><span class="line"># <span class="keyword">is</span> <span class="keyword">null</span>：查询 没有<span class="operator">/</span>有 奖金的员工名</span><br><span class="line"><span class="keyword">SELECT</span> last_name , commission_pct <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> commission_pct <span class="keyword">IS</span> <span class="keyword">NULL</span>;</span><br><span class="line"><span class="keyword">SELECT</span> last_name , commission_pct <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> commission_pct <span class="keyword">IS</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span>;</span><br><span class="line"></span><br><span class="line"># 安全等于 <span class="operator">&lt;=&gt;</span></span><br><span class="line"></span><br><span class="line"># 查询没有奖金的员工名和奖金</span><br><span class="line"><span class="keyword">SELECT</span> last_name , commission_pct <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> commission_pct <span class="operator">&lt;=&gt;</span> <span class="keyword">NULL</span>;</span><br><span class="line"></span><br><span class="line"># 查询工资为<span class="number">12000</span>的员工信息</span><br><span class="line"><span class="keyword">SELECT</span> last_name , salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> salary <span class="operator">&lt;=&gt;</span> <span class="number">12000</span>;</span><br></pre></td></tr></table></figure>
<h2 id="3、排序查询"><a href="#3、排序查询" class="headerlink" title="3、排序查询"></a>3、排序查询</h2><blockquote>
<p><strong>语法</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">		进阶三：排序查询</span></span><br><span class="line"><span class="comment">			语法：</span></span><br><span class="line"><span class="comment">				SELECT 查询列表 FROM 表 【WHERE 筛选条件】 ORDER BY 排序列表 【ASC / DESC】</span></span><br><span class="line"><span class="comment">				ASC：升序。DESC：降序。如果不写，默认是升序</span></span><br><span class="line"><span class="comment">				ORDER BY 子句中可以支持单个字段，多个字段，表达式，函数，别名来进行排序</span></span><br><span class="line"><span class="comment">				ORDER BY 子句一般是放在查询语句的最后面，LIMIT子句放在 ORDER BY后面</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>案例</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">		进阶三：排序查询</span></span><br><span class="line"><span class="comment">			语法：</span></span><br><span class="line"><span class="comment">				SELECT 查询列表 FROM 表 【WHERE 筛选条件】 ORDER BY 排序列表 【ASC / DESC】</span></span><br><span class="line"><span class="comment">				ASC：升序。DESC：降序。如果不写，默认是升序</span></span><br><span class="line"><span class="comment">				ORDER BY 子句中可以支持单个字段，多个字段，表达式，函数，别名来进行排序</span></span><br><span class="line"><span class="comment">				ORDER BY 子句一般是放在查询语句的最后面，LIMIT子句放在 ORDER BY后面</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># 查询员工信息，要求工资从高到低进行排序</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">ORDER</span> <span class="keyword">BY</span> salary <span class="keyword">DESC</span>;</span><br><span class="line"></span><br><span class="line"># 查询部门编号<span class="operator">&gt;=</span><span class="number">90</span>的员工信息，按入职时间的先后进行排序【添加筛选条件】</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> department_id <span class="operator">&gt;=</span> <span class="number">90</span> <span class="keyword">ORDER</span> <span class="keyword">BY</span> hiredate <span class="keyword">ASC</span>;</span><br><span class="line"></span><br><span class="line"># 按年薪的高低显示员工的信息和年薪【按表达式（别名）排序】</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span>,salary<span class="operator">*</span><span class="number">12</span><span class="operator">*</span>(<span class="number">1</span> <span class="operator">+</span> IFNULL(commission_pct,<span class="number">0</span>)) 年薪 <span class="keyword">FROM</span> employees <span class="keyword">ORDER</span> <span class="keyword">BY</span> 年薪 <span class="keyword">DESC</span>;</span><br><span class="line"></span><br><span class="line"># 按姓名的长度显示员工的姓名和工资【按函数排序】</span><br><span class="line"><span class="keyword">SELECT</span> LENGTH(last_name) 名字长度 <span class="keyword">FROM</span> employees <span class="keyword">ORDER</span> <span class="keyword">BY</span> 名字长度 <span class="keyword">ASC</span>;</span><br><span class="line"></span><br><span class="line"># 查询员工信息，要求先按员工工资排序升序，再按员工编号排序降序【多个字段排序】</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">ORDER</span> <span class="keyword">BY</span> salary <span class="keyword">ASC</span> , employee_id <span class="keyword">DESC</span>;	</span><br></pre></td></tr></table></figure>
<h2 id="4、单行函数"><a href="#4、单行函数" class="headerlink" title="4、单行函数"></a>4、单行函数</h2><h3 id="4-1、常见函数"><a href="#4-1、常见函数" class="headerlink" title="4.1、常见函数"></a>4.1、常见函数</h3><blockquote>
<p><strong>语法</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">		进阶四：常见函数</span></span><br><span class="line"><span class="comment">		功能：类似与Java中的方法，将一组逻辑语句将其封装再方法体中，对外暴露方法名</span></span><br><span class="line"><span class="comment">		好处：	</span></span><br><span class="line"><span class="comment">			1、隐藏了细节</span></span><br><span class="line"><span class="comment">			2、提高代码的重用性</span></span><br><span class="line"><span class="comment">		调用：SELECT 函数名() 【FROM 表（当用到表用的字段拼接该语句）】</span></span><br><span class="line"><span class="comment">		特点：</span></span><br><span class="line"><span class="comment">			1、叫什么：函数名</span></span><br><span class="line"><span class="comment">			2、干什么：函数功能</span></span><br><span class="line"><span class="comment">		分类；</span></span><br><span class="line"><span class="comment">			1、单行函数</span></span><br><span class="line"><span class="comment">				如：CONCAT，LENGTH，IFNULL等</span></span><br><span class="line"><span class="comment">			2、分组函数</span></span><br><span class="line"><span class="comment">				功能：做统计使用，又称为统计函数，聚合函数，组函数</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>案例</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">		进阶四：常见函数</span></span><br><span class="line"><span class="comment">		功能：类似与Java中的方法，将一组逻辑语句将其封装再方法体中，对外暴露方法名</span></span><br><span class="line"><span class="comment">		好处：	</span></span><br><span class="line"><span class="comment">			1、隐藏了细节</span></span><br><span class="line"><span class="comment">			2、提高代码的重用性</span></span><br><span class="line"><span class="comment">		调用：SELECT 函数名() 【FROM 表（当用到表用的字段拼接该语句）】</span></span><br><span class="line"><span class="comment">		特点：</span></span><br><span class="line"><span class="comment">			1、叫什么：函数名</span></span><br><span class="line"><span class="comment">			2、干什么：函数功能</span></span><br><span class="line"><span class="comment">		分类；</span></span><br><span class="line"><span class="comment">			1、单行函数</span></span><br><span class="line"><span class="comment">				如：CONCAT，LENGTH，IFNULL等</span></span><br><span class="line"><span class="comment">			2、分组函数</span></span><br><span class="line"><span class="comment">				功能：做统计使用，又称为统计函数，聚合函数，组函数</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># <span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span>字符函数<span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span></span><br><span class="line"></span><br><span class="line"># LENGTH：字符串长度【一个英文占一个字节，常见中文占三个字节，复杂的中文字节数可能是三个以上】</span><br><span class="line"><span class="keyword">SELECT</span> LENGTH(<span class="string">&#x27;aaa&#x27;</span>);</span><br><span class="line"><span class="keyword">SELECT</span> LENGTH(<span class="string">&#x27;张三丰&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># 查看字符集</span><br><span class="line"><span class="keyword">SHOW</span> VARIABLES <span class="keyword">LIKE</span> <span class="string">&#x27;%char%&#x27;</span>;</span><br><span class="line"></span><br><span class="line"># CONCAT：字符串拼接</span><br><span class="line"><span class="keyword">SELECT</span> CONCAT(last_name,<span class="string">&#x27;-&#x27;</span>,first_name) <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># <span class="built_in">UPPER</span>(将小写转换为大写)、<span class="built_in">LOWER</span>(将大写转换为小写)</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">UPPER</span>(<span class="string">&#x27;aaa&#x27;</span>);</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">LOWER</span>(<span class="string">&#x27;AAAb&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># 将姓变大写，将名变小写</span><br><span class="line"><span class="keyword">SELECT</span> CONCAT(<span class="built_in">UPPER</span>(last_name),<span class="string">&#x27;-&#x27;</span>,<span class="built_in">LOWER</span>(first_name)) 姓名 <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># SUBSTR、SUBSTRING：字符串截取</span><br><span class="line"><span class="keyword">SELECT</span> SUBSTR(<span class="string">&#x27;李莫愁&#x27;</span>,<span class="number">3</span>);</span><br><span class="line"><span class="keyword">SELECT</span> SUBSTR(<span class="string">&#x27;李莫愁&#x27;</span>,<span class="number">1</span>,<span class="number">2</span>) ;</span><br><span class="line"></span><br><span class="line"># 姓名中首字符大写，其他字符小写然后用<span class="operator">-</span>拼接，显示出来</span><br><span class="line"><span class="keyword">SELECT</span> CONCAT(<span class="built_in">UPPER</span>(SUBSTR(last_name,<span class="number">1</span>,<span class="number">1</span>)),<span class="string">&#x27;-&#x27;</span>,<span class="built_in">LOWER</span>(SUBSTR(last_name,<span class="number">2</span>))) <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># INSTR：获取子串第一次出现的索引，如果找不到返回<span class="number">0</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">INSERT</span>(<span class="string">&#x27;杨不悔aaa&#x27;</span>,);</span><br><span class="line"></span><br><span class="line"># TRIM：去除字符串前后的空格</span><br><span class="line"><span class="keyword">SELECT</span> LENGTH(<span class="built_in">TRIM</span>(<span class="string">&#x27;  aaaa    &#x27;</span>));</span><br><span class="line"></span><br><span class="line"># TRIM：去除字符串前后指定的字符</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">TRIM</span>(<span class="string">&#x27;a&#x27;</span> <span class="keyword">FROM</span> <span class="string">&#x27;abaaaaaaaaba&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># LPAD：用指定的字符实现左填充指定长度</span><br><span class="line"><span class="keyword">SELECT</span> LPAD(<span class="string">&#x27;aaa&#x27;</span>,<span class="number">10</span>,<span class="string">&#x27;*&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># RPAD：用指定的字符实现右填充指定长度</span><br><span class="line"><span class="keyword">SELECT</span> RPAD(<span class="string">&#x27;aaa&#x27;</span>,<span class="number">10</span>,<span class="string">&#x27;*&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># REPLACE：将指定字符替换为新的字符</span><br><span class="line"><span class="keyword">SELECT</span> REPLACE(<span class="string">&#x27;aaaaaddddddcccccc&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;b&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># <span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span>数学函数<span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span></span><br><span class="line"></span><br><span class="line"># ROUND：四舍五入</span><br><span class="line"><span class="keyword">SELECT</span> ROUND(<span class="number">5.55555</span>,<span class="number">2</span>)；</span><br><span class="line"></span><br><span class="line"># CEIL：向上取整，返回 <span class="operator">&gt;=</span> 该参数的最小整数</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">CEIL</span>(<span class="number">1.02</span>);</span><br><span class="line"></span><br><span class="line"># 向下取整，返回 <span class="operator">&lt;=</span> 该参数的最大整数</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">FLOOR</span>(<span class="number">1.02</span>);</span><br><span class="line"></span><br><span class="line"># <span class="keyword">TRUNCATE</span>：截断</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">TRUNCATE</span>(<span class="number">1.9999</span>,<span class="number">1</span>); # 输出；<span class="number">1.9</span></span><br><span class="line"></span><br><span class="line"># MOD：取余</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MOD</span>(<span class="number">10</span>,<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"># <span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span>日期函数<span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span></span><br><span class="line"></span><br><span class="line"># NOW：返回当前系统日期<span class="operator">+</span>时间</span><br><span class="line"><span class="keyword">SELECT</span> NOW();</span><br><span class="line"></span><br><span class="line"># CURDATE：返回当前系统日期，不包含时间</span><br><span class="line"><span class="keyword">SELECT</span> CURDATE();</span><br><span class="line"></span><br><span class="line"># CURTIME：返回当前时间，不包含日期</span><br><span class="line"><span class="keyword">SELECT</span> CURTIME();</span><br><span class="line"></span><br><span class="line"># 获取指定的部分，年、月、日、小时、分钟、秒</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">YEAR</span>(NOW());</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">YEAR</span>(<span class="string">&#x27;1999-1-1&#x27;</span>);</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">MONTH</span>(NOW());</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">MONTH</span>(<span class="string">&#x27;1999-1-1&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># STR_TO_DATE：将日期字符串转换为日期</span><br><span class="line"><span class="keyword">SELECT</span> STR_TO_DATE(<span class="string">&#x27;1999-10-8&#x27;</span>,<span class="string">&#x27;%y-%c-%d&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># DATE_FORMAT：将日期转换为字符串</span><br><span class="line"><span class="keyword">SELECT</span> DATE_FORMAT(CURDATE(),<span class="string">&#x27;%y年%m月%d日&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># 查询有奖金的员工名和入职日期（xx月<span class="operator">/</span>xx日 xx年）</span><br><span class="line"><span class="keyword">SELECT</span> last_name , DATE_FORMAT(hiredate,<span class="string">&#x27;%m月/%d日 %y年&#x27;</span>) 入职日期 <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># <span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span>其他函数<span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span> VERSION(); # 查询数据库版本</span><br><span class="line"><span class="keyword">SELECT</span> DATABASE(); # 查询数据库</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">USER</span>(); # 查询用户</span><br><span class="line"></span><br><span class="line"># <span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span>流程控制函数<span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span> IF(<span class="number">10</span><span class="operator">&gt;</span><span class="number">4</span>,<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span> last_name,commission_pct ,IF(commission_pct <span class="keyword">IS</span> <span class="keyword">NULL</span>,<span class="string">&#x27;没奖金&#x27;</span>,<span class="string">&#x27;有奖金&#x27;</span>) 备注 <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># <span class="keyword">CASE</span>函数的使用一：类似于Java中的switch、<span class="keyword">case</span>的效果</span><br><span class="line"></span><br><span class="line"># 查询员工的工资，要求</span><br><span class="line"># 部门<span class="operator">=</span><span class="number">30</span>，显示的工资为<span class="number">1.1</span>倍</span><br><span class="line"># 部门<span class="operator">=</span><span class="number">40</span>号，显示工资为<span class="number">1.2</span>倍</span><br><span class="line"># 部门<span class="operator">=</span><span class="number">50</span>，显示工资为<span class="number">1.2</span>倍</span><br><span class="line"># 其他部门，显示的工资为原工资</span><br><span class="line"><span class="keyword">SELECT</span> salary 原始工资,department_id,</span><br><span class="line"><span class="keyword">CASE</span> department_id </span><br><span class="line">		<span class="keyword">WHEN</span> <span class="number">30</span> <span class="keyword">THEN</span> salary <span class="operator">*</span> <span class="number">1.1</span> </span><br><span class="line">		<span class="keyword">WHEN</span> <span class="number">40</span> <span class="keyword">THEN</span> salary <span class="operator">*</span> <span class="number">1.2</span></span><br><span class="line">		<span class="keyword">WHEN</span> <span class="number">50</span> <span class="keyword">THEN</span> salary <span class="operator">*</span> <span class="number">1.3</span> </span><br><span class="line">		<span class="keyword">ELSE</span> salary </span><br><span class="line"><span class="keyword">END</span> <span class="keyword">AS</span> 新工资</span><br><span class="line"><span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># <span class="keyword">CASE</span>函数的使用二：类似于Java中的多重if</span><br><span class="line"></span><br><span class="line"># 查询员工的工资情况</span><br><span class="line"><span class="keyword">SELECT</span> salary,</span><br><span class="line"><span class="keyword">CASE</span></span><br><span class="line"><span class="keyword">WHEN</span> salary <span class="operator">&gt;</span> <span class="number">10000</span> <span class="keyword">THEN</span> <span class="string">&#x27;A&#x27;</span></span><br><span class="line"><span class="keyword">WHEN</span> salary <span class="operator">&gt;</span> <span class="number">15000</span> <span class="keyword">THEN</span> <span class="string">&#x27;B&#x27;</span></span><br><span class="line"><span class="keyword">WHEN</span> salary <span class="operator">&gt;</span> <span class="number">20000</span> <span class="keyword">THEN</span> <span class="string">&#x27;C&#x27;</span></span><br><span class="line"><span class="keyword">ELSE</span> <span class="string">&#x27;D&#x27;</span></span><br><span class="line"><span class="keyword">END</span> <span class="keyword">AS</span> 工资级别</span><br><span class="line"><span class="keyword">FROM</span> employees;</span><br></pre></td></tr></table></figure>
<h3 id="4-2、单行函数总结"><a href="#4-2、单行函数总结" class="headerlink" title="4.2、单行函数总结"></a>4.2、单行函数总结</h3><blockquote>
<p><strong>函数总结</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">常见函数总结：</span></span><br><span class="line"><span class="comment">	字符函数：</span></span><br><span class="line"><span class="comment">		length、concat、substr、instr、trim、upper、lower、lpad、rpad、replace</span></span><br><span class="line"><span class="comment">	数学函数：</span></span><br><span class="line"><span class="comment">		round、ceil、floor、truncate、mod</span></span><br><span class="line"><span class="comment">	日期函数：</span></span><br><span class="line"><span class="comment">		now、curdate、curtime、year、month、monthname、day、hour、minute、second、str_to_date、date_format、</span></span><br><span class="line"><span class="comment">	其他函数：</span></span><br><span class="line"><span class="comment">		version、database、user</span></span><br><span class="line"><span class="comment">	控制函数：</span></span><br><span class="line"><span class="comment">		if、case</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>例</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"># <span class="number">1</span>、显示系统时间（注：日期<span class="operator">+</span>时间）</span><br><span class="line"><span class="keyword">SELECT</span> NOW();</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、查询员工号、姓名、工资、以及工资提高<span class="number">20</span><span class="operator">%</span>之后的结果（<span class="keyword">new</span> salary）</span><br><span class="line"><span class="keyword">SELECT</span> employee_id,last_name,salary,salary<span class="operator">*</span><span class="number">1.2</span> &quot;new salary&quot; <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># <span class="number">3</span>、将员工的姓名按首字母排序，并写出姓名的长度（length）</span><br><span class="line"><span class="keyword">SELECT</span> LENGTH(last_name),SUBSTR(last_name,<span class="number">1</span>,<span class="number">1</span>) 首字符 ,last_name <span class="keyword">FROM</span> employees <span class="keyword">ORDER</span> <span class="keyword">BY</span> 首字符;</span><br><span class="line"></span><br><span class="line"># <span class="number">4</span>、做一个查询，产生以下的结果</span><br><span class="line"># <span class="operator">&lt;</span>last_name<span class="operator">&gt;</span> earns <span class="operator">&lt;</span>salart<span class="operator">&gt;</span> monthly but wants <span class="operator">&lt;</span>salary<span class="operator">*</span><span class="number">3</span><span class="operator">&gt;</span></span><br><span class="line"><span class="keyword">SELECT</span> CONCAT(last_name,<span class="string">&#x27; earns &#x27;</span>,salary,<span class="string">&#x27; monthly but wants &#x27;</span>,salary<span class="operator">*</span><span class="number">3</span>) <span class="keyword">AS</span> &quot;Dream Salary&quot; <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># <span class="number">5</span>、使用<span class="keyword">case</span><span class="operator">-</span><span class="keyword">when</span>，按照下面的条件</span><br><span class="line"><span class="keyword">SELECT</span> job_id <span class="keyword">AS</span> job,</span><br><span class="line"><span class="keyword">CASE</span> job_id</span><br><span class="line">	<span class="keyword">WHEN</span> <span class="string">&#x27;AD_PRES&#x27;</span> <span class="keyword">THEN</span> <span class="string">&#x27;A&#x27;</span></span><br><span class="line">	<span class="keyword">WHEN</span> <span class="string">&#x27;ST_MAN&#x27;</span> <span class="keyword">THEN</span> <span class="string">&#x27;B&#x27;</span></span><br><span class="line">	<span class="keyword">WHEN</span> <span class="string">&#x27;IT_PR OG&#x27;</span> <span class="keyword">THEN</span> <span class="string">&#x27;C&#x27;</span></span><br><span class="line">	<span class="keyword">WHEN</span> <span class="string">&#x27;SA_PRE&#x27;</span> <span class="keyword">THEN</span> <span class="string">&#x27;D&#x27;</span></span><br><span class="line">	<span class="keyword">WHEN</span> <span class="string">&#x27;ST_CLERK&#x27;</span> <span class="keyword">THEN</span> <span class="string">&#x27;E&#x27;</span></span><br><span class="line"><span class="keyword">END</span> <span class="keyword">AS</span> Grade</span><br><span class="line"><span class="keyword">FROM</span> employees;</span><br></pre></td></tr></table></figure>
<h2 id="5、分组函数"><a href="#5、分组函数" class="headerlink" title="5、分组函数"></a>5、分组函数</h2><h3 id="5-1、常见函数"><a href="#5-1、常见函数" class="headerlink" title="5.1、常见函数"></a>5.1、常见函数</h3><blockquote>
<p><strong>语法</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"># 分组函数</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">功能：用作统计使用，又称为聚合函数或统计函数或组函数</span></span><br><span class="line"><span class="comment">分类：</span></span><br><span class="line"><span class="comment">	sum：求和、avg：平均值、max：最大值、min：最小值、count：计算不为null的个数</span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">	1、sum、avg一般用于处理数值型</span></span><br><span class="line"><span class="comment">	2、max、min、count可以处理任何类型</span></span><br><span class="line"><span class="comment">	3、以上分组函数都忽略null值</span></span><br><span class="line"><span class="comment">	4、可以和 DISTINCT 搭配实现去重</span></span><br><span class="line"><span class="comment">	5、count函数的单独介绍：一般count(*)用作统计函数</span></span><br><span class="line"><span class="comment">	5、和分组函数一同查询的字段要求是group by 后的字段</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、简单使用</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">SUM</span>(salary) 求和 , <span class="built_in">AVG</span>(salary) 平均值 , <span class="built_in">MIN</span>(salary) 最小值 , <span class="built_in">MAX</span>(salary) 最大值 , <span class="built_in">COUNT</span>(salary) 统计 <span class="keyword">FROM</span> employees; </span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、count函数的具体使用</span><br><span class="line"></span><br><span class="line"># (<span class="number">1</span>)用于统计指定字段中非空的总行数</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(salary) <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># (<span class="number">2</span>)用于统计总行数</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># (<span class="number">3</span>) 结果一般和<span class="built_in">COUNT</span>(<span class="operator">*</span>)一样</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="number">1</span>) <span class="keyword">FROM</span> employees;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">效率：</span></span><br><span class="line"><span class="comment">MISAM存储引擎下：COUNT(*)的效率高</span></span><br><span class="line"><span class="comment">INNODB存储引擎下：COUNT(*)和COUNT(1)的效率差不多，比COUNT(字段)要高一些</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># <span class="number">3</span>、和分组函数一同查询的字段有限制</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">AVG</span>(salary) ,employee_id <span class="keyword">FROM</span> employees;</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>案例</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># <span class="number">1</span>、查询公司员工的最大值，最小值，平均值，总和</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary) max_sal,<span class="built_in">MIN</span>(salary) min_sal,ROUND(<span class="built_in">AVG</span>(salary),<span class="number">2</span>) avg_sal,<span class="built_in">SUM</span>(salary) sum_sal <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、查询员工表中的最大入职时间和最小入职时间的相差天数(DATEDIFF)</span><br><span class="line"><span class="keyword">SELECT</span> DATEDIFF(<span class="built_in">MAX</span>(hiredate),<span class="built_in">MIN</span>(hiredate)) DIFFRENCE <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># <span class="number">3</span>、查询部门编号为<span class="number">90</span>的员工个数</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> department_id <span class="operator">=</span> <span class="number">90</span>;</span><br></pre></td></tr></table></figure>
<h3 id="5-2、分组查询"><a href="#5-2、分组查询" class="headerlink" title="5.2、分组查询"></a>5.2、分组查询</h3><blockquote>
<p><strong>语法和案例</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"># 进阶查询：分组查询</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法</span></span><br><span class="line"><span class="comment">	SELECT 分组函数 , 列（要求出现在group by的后面）</span></span><br><span class="line"><span class="comment">	from 表</span></span><br><span class="line"><span class="comment">	【where 筛选条件】</span></span><br><span class="line"><span class="comment">	group by 分组的列表</span></span><br><span class="line"><span class="comment">	【order by 子句】</span></span><br><span class="line"><span class="comment">注意：</span></span><br><span class="line"><span class="comment">	查询的列表必须特殊，要求是分组函数和group by 后出现的字段</span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">	1、分组查询中的筛选按条件分为两类</span></span><br><span class="line"><span class="comment">					数据源				  	位置													关键字</span></span><br><span class="line"><span class="comment">		分组前筛选：  原始表				  	GROUP BY 子句前面										WHERE</span></span><br><span class="line"><span class="comment">		分组后筛选	 分组后的结果集    	  GROUP BY 子句的后面									HAVING</span></span><br><span class="line"><span class="comment">		分组函数做条件肯定是放在HAVING子句中</span></span><br><span class="line"><span class="comment">		能用分组前筛选的，就优先考虑使用分组前筛选</span></span><br><span class="line"><span class="comment">	2、GROUP BY 子句支持单个字段分组，多个字段分组（多个字段之间用逗号，没有顺序要求），表达式或函数（使用较少）</span></span><br><span class="line"><span class="comment">	3、可以添加排序（排序放在整个分组查询的后面）</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、查询每个工种的最高工资</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary),job_id <span class="keyword">FROM</span> employees <span class="keyword">GROUP</span> <span class="keyword">BY</span> job_id;</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、查询每个位置上的部门个数</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>),location_id <span class="keyword">FROM</span> departments <span class="keyword">GROUP</span> <span class="keyword">BY</span> location_id;</span><br><span class="line"></span><br><span class="line"># <span class="number">3</span>、添加筛选条件：</span><br><span class="line"></span><br><span class="line"># <span class="number">3.1</span>、查询邮箱中包含a字符的，每个部门的平均工资</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">AVG</span>(salary),department_id <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> email <span class="keyword">LIKE</span> <span class="string">&#x27;%a%&#x27;</span> <span class="keyword">GROUP</span> <span class="keyword">BY</span> department_id;</span><br><span class="line"></span><br><span class="line"># <span class="number">3.2</span>、查询有奖金的每个领导手下员工的最高工资</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary),manager_id <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> commission_pct <span class="keyword">IS</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">GROUP</span> <span class="keyword">BY</span> manager_id;</span><br><span class="line"></span><br><span class="line"># <span class="number">3.3</span>、查询哪个部门的员工个数<span class="operator">&gt;</span><span class="number">2</span></span><br><span class="line"># 步骤：</span><br><span class="line"># 	①查询每个部门的员工个数</span><br><span class="line"># 	②根据①的结果进行筛选，查询哪个部门的员工个数<span class="operator">&gt;</span><span class="number">2</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>),department_id <span class="keyword">FROM</span> employees <span class="keyword">GROUP</span> <span class="keyword">BY</span> department_id <span class="keyword">HAVING</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>)<span class="operator">&gt;</span><span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">3.4</span>、查询每个工种有奖金的员工的最高工资<span class="operator">&gt;</span><span class="number">12000</span>的工种编号和最高工资</span><br><span class="line"># ①查询每个工种有奖金的员工的最高工资</span><br><span class="line"># ②根据①的结果进行筛选，最高工资<span class="operator">&gt;</span><span class="number">12000</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary),job_id <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> commission_pct <span class="keyword">IS</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">GROUP</span> <span class="keyword">BY</span> job_id <span class="keyword">HAVING</span> <span class="built_in">MAX</span>(salary)<span class="operator">&gt;</span><span class="number">12000</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">3.5</span>、查询领导编号<span class="operator">&gt;</span><span class="number">102</span>的每个领导手下的最低工资<span class="operator">&gt;</span><span class="number">5000</span>的领导编号是哪个，以及最低工资</span><br><span class="line"># ①查询每个领导手下的员工固定最低工资</span><br><span class="line"># ②添加筛选条件：编号<span class="operator">&gt;</span><span class="number">102</span></span><br><span class="line"># ③最低工资大于<span class="number">5000</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MIN</span>(salary),manager_id <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> manager_id<span class="operator">&gt;</span><span class="number">102</span> <span class="keyword">GROUP</span> <span class="keyword">BY</span> manager_id <span class="keyword">HAVING</span> <span class="built_in">MIN</span>(salary)<span class="operator">&gt;</span><span class="number">5000</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">4</span>、按表达式或函数进行分组</span><br><span class="line"># 按员工姓名的长度分组，查询每一组员工个数，筛选员工个数<span class="operator">&gt;</span><span class="number">5</span>的有哪些</span><br><span class="line"># ①查询每个长度的员工个数</span><br><span class="line"># ②添加筛选条件</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>),LENGTH(last_name) <span class="keyword">FROM</span> employees <span class="keyword">GROUP</span> <span class="keyword">BY</span> LENGTH(last_name) <span class="keyword">HAVING</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>)<span class="operator">&gt;</span><span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"># 按多个字段分组</span><br><span class="line"># 查询每个部门每个工种的员工的平均工资</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">AVG</span>(salary) ,department_id ,job_id <span class="keyword">FROM</span> employees <span class="keyword">GROUP</span> <span class="keyword">BY</span> job_id,department_id;</span><br><span class="line"></span><br><span class="line"># 添加排序</span><br><span class="line"># 查询每个部门工种的员工的平均工资，并且按平均工资的高低显示</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">AVG</span>(salary),department_id,job_id <span class="keyword">FROM</span> employees <span class="keyword">GROUP</span> <span class="keyword">BY</span> job_id,department_id <span class="keyword">ORDER</span> <span class="keyword">BY</span> <span class="built_in">AVG</span>(salary) <span class="keyword">DESC</span>;\</span><br><span class="line"></span><br><span class="line"># 添加排序</span><br><span class="line"># 查询每个部门工种的员工的平均工资，并且按平均工资的高低显示</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">AVG</span>(salary),department_id,job_id <span class="keyword">FROM</span> employees <span class="keyword">GROUP</span> <span class="keyword">BY</span> job_id,department_id <span class="keyword">HAVING</span> <span class="built_in">AVG</span>(salary)<span class="operator">&gt;</span><span class="number">10000</span> <span class="keyword">ORDER</span> <span class="keyword">BY</span> <span class="built_in">AVG</span>(salary) <span class="keyword">DESC</span>;</span><br></pre></td></tr></table></figure>
<h2 id="6、连接查询"><a href="#6、连接查询" class="headerlink" title="6、连接查询"></a>6、连接查询</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">含义：又称多表查询，当查询的字段来自多个表时，就会用到连接查询</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">笛卡尔乘积现象：表1 有m行，表2 有n行，结果 = m*n行</span></span><br><span class="line"><span class="comment">发生笛卡尔积现象，没有有效的连接条件</span></span><br><span class="line"><span class="comment">如何避免：添加上有效的连接条件</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">分类：</span></span><br><span class="line"><span class="comment">	按年代分类：</span></span><br><span class="line"><span class="comment">		sql92标准：仅支持内连接</span></span><br><span class="line"><span class="comment">		sql99标准：支持 内连接+外连接（左外和右外）+交叉连接【推荐】</span></span><br><span class="line"><span class="comment">	按功能分类：</span></span><br><span class="line"><span class="comment">		内连接：用于查询多张表的交集</span></span><br><span class="line"><span class="comment">			等值查询</span></span><br><span class="line"><span class="comment">			非等值查询</span></span><br><span class="line"><span class="comment">		外连接：</span></span><br><span class="line"><span class="comment">			左外连接</span></span><br><span class="line"><span class="comment">			右外连接</span></span><br><span class="line"><span class="comment">			全外连接</span></span><br><span class="line"><span class="comment">		交叉连接：</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="6-1、sql92"><a href="#6-1、sql92" class="headerlink" title="6.1、sql92"></a>6.1、sql92</h3><h4 id="6-1-1、等值查询"><a href="#6-1-1、等值查询" class="headerlink" title="6.1.1、等值查询"></a>6.1.1、等值查询</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"># 连接查询</span><br><span class="line"></span><br><span class="line"># 一、sql92标准</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、等值查询：</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	①多表等值连接的结果为多表的交集部分</span></span><br><span class="line"><span class="comment">	②n表连接 ，至少需要n-1个连接条件</span></span><br><span class="line"><span class="comment">	③多表的顺序没有要求</span></span><br><span class="line"><span class="comment">	④一般需要为表起别名</span></span><br><span class="line"><span class="comment">	⑤可以搭配前面所学的所有子句使用，比如排序，分组，筛选等</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># （<span class="number">1</span>）查询女生名和对应的男生名 </span><br><span class="line"><span class="keyword">SELECT</span> `name`,boyName <span class="keyword">FROM</span> boys,beauty <span class="keyword">WHERE</span> boys.id <span class="operator">=</span> beauty.boyfriend_id;</span><br><span class="line"></span><br><span class="line"># （<span class="number">2</span>）查询员工名和对应的部门名</span><br><span class="line"><span class="keyword">SELECT</span> last_name,department_name <span class="keyword">FROM</span> employees,departments <span class="keyword">WHERE</span> employees.department_id<span class="operator">=</span>departments.department_id;</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、为表起别名【表名 [<span class="keyword">as</span>] 别名】，提高语句的简洁度，区分多个重名字段</span><br><span class="line"></span><br><span class="line"># （<span class="number">1</span>）查询员工名，工种号。工种名</span><br><span class="line"><span class="keyword">SELECT</span> last_name,a.job_id,job_title <span class="keyword">FROM</span> employees a,jobs b <span class="keyword">WHERE</span> a.job_id<span class="operator">=</span>b.job_id;</span><br><span class="line"></span><br><span class="line"># <span class="number">3</span>、添加筛选</span><br><span class="line"></span><br><span class="line"># （<span class="number">1</span>）查询有奖金的员工名，部门名</span><br><span class="line"><span class="keyword">SELECT</span> last_name,commission_pct,department_name <span class="keyword">FROM</span> employees a,departments b <span class="keyword">WHERE</span> a.department_id<span class="operator">=</span>b.department_id <span class="keyword">AND</span> a.commission_pct <span class="keyword">IS</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span>;</span><br><span class="line"></span><br><span class="line"># （<span class="number">2</span>）查询城市名中第二个字符为o的部门名和城市名</span><br><span class="line"><span class="keyword">SELECT</span> department_name,city <span class="keyword">FROM</span> departments d,locations l <span class="keyword">WHERE</span> d.location_id <span class="operator">=</span> l.location_id <span class="keyword">AND</span> city <span class="keyword">LIKE</span> <span class="string">&#x27;_o%&#x27;</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">4</span>、添加分组</span><br><span class="line"></span><br><span class="line"># （<span class="number">1</span>）查询每个城市的部门个数</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) 个数 , city <span class="keyword">FROM</span> departments d,locations l <span class="keyword">WHERE</span> d.location_id <span class="operator">=</span> l.location_id <span class="keyword">GROUP</span> <span class="keyword">BY</span> city;</span><br><span class="line"></span><br><span class="line"># （<span class="number">2</span>）查询有奖金的每个部门的部门名和部门的领导编号和该部门的最低工资</span><br><span class="line"><span class="keyword">SELECT</span> department_name,d.manager_id,<span class="built_in">MIN</span>(salary) <span class="keyword">FROM</span> departments d,employees e <span class="keyword">WHERE</span> d.department_id<span class="operator">=</span>e.department_id</span><br><span class="line"><span class="keyword">AND</span> commission_pct <span class="keyword">IS</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">GROUP</span> <span class="keyword">BY</span> department_name,d.manager_id;</span><br><span class="line"></span><br><span class="line"># <span class="number">5</span>、添加排序</span><br><span class="line"></span><br><span class="line"># （<span class="number">1</span>）查询每个工种的工种名和员工个数， 并且按员工个数降序</span><br><span class="line"><span class="keyword">SELECT</span> job_title,<span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="keyword">FROM</span> employees e,jobs j <span class="keyword">WHERE</span> e.job_id<span class="operator">=</span>j.job_id <span class="keyword">GROUP</span> <span class="keyword">BY</span> job_title <span class="keyword">ORDER</span> <span class="keyword">BY</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="keyword">DESC</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">6</span>、实现三表连接</span><br><span class="line"></span><br><span class="line"># （<span class="number">1</span>）查询员工名、部门名和所在城市</span><br><span class="line"><span class="keyword">SELECT</span> last_name,department_name,city <span class="keyword">FROM</span> employees e,departments d,locations l <span class="keyword">WHERE</span> e.department_id<span class="operator">=</span>d.department_id <span class="keyword">AND</span> d.location_id<span class="operator">=</span>l.location_id <span class="keyword">AND</span> city <span class="keyword">LIKE</span> <span class="string">&#x27;s%&#x27;</span> <span class="keyword">ORDER</span> <span class="keyword">BY</span> department_name <span class="keyword">DESC</span>;</span><br></pre></td></tr></table></figure>
<h4 id="6-1-2、非等值查询"><a href="#6-1-2、非等值查询" class="headerlink" title="6.1.2、非等值查询"></a>6.1.2、非等值查询</h4><p><strong>非等值查询一般为不是<code>=</code>来查询，使用<code>&gt;、&lt;等查询</code></strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 二、非等值查询</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、查询员工的工资和工资级别</span><br><span class="line"><span class="keyword">SELECT</span> e.salary,grade_level <span class="keyword">FROM</span> employees e,job_grades g <span class="keyword">WHERE</span> e.salary <span class="keyword">BETWEEN</span> g.lowest_sal <span class="keyword">AND</span> g.highest_sal <span class="keyword">AND</span> g.grade_level<span class="operator">=</span><span class="string">&#x27;A&#x27;</span>;</span><br></pre></td></tr></table></figure>
<h4 id="6-1-3、自连接"><a href="#6-1-3、自连接" class="headerlink" title="6.1.3、自连接"></a>6.1.3、自连接</h4><p><strong>和等值查询类似，自连接是自己连接自己，例如三级分类表</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 自连接</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、查询员工名和上级</span><br><span class="line"><span class="keyword">SELECT</span> e.last_name ,m.last_name  <span class="keyword">FROM</span> employees e,employees m <span class="keyword">WHERE</span> e.manager_id<span class="operator">=</span>m.employee_id;</span><br></pre></td></tr></table></figure>
<h3 id="6-2、sql99"><a href="#6-2、sql99" class="headerlink" title="6.2、sql99"></a>6.2、sql99</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"># 二、sql99语法</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法</span></span><br><span class="line"><span class="comment">	SELECT 查询列表 </span></span><br><span class="line"><span class="comment">	FROM 表1 别名 【连接类型】 </span></span><br><span class="line"><span class="comment">	JOIN 表2 别名</span></span><br><span class="line"><span class="comment">	ON 连接条件</span></span><br><span class="line"><span class="comment">	WHERE 筛选条件</span></span><br><span class="line"><span class="comment">	</span></span><br><span class="line"><span class="comment">内连接（⭐）：INNER</span></span><br><span class="line"><span class="comment">外连接（⭐）</span></span><br><span class="line"><span class="comment">	左外（⭐）：LEFT【OUTER】</span></span><br><span class="line"><span class="comment">	右外（⭐）：RIGHT【OUTER】</span></span><br><span class="line"><span class="comment">	全外：FULL【OUTER】</span></span><br><span class="line"><span class="comment">交叉连接：CROSS</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">	①添加排序、分组、筛选</span></span><br><span class="line"><span class="comment">	②inner可以省略</span></span><br><span class="line"><span class="comment">	③筛选条件可以放在where后面，连接条件放在on后面，提高分离性，便于阅读</span></span><br><span class="line"><span class="comment">	④inner join 连接可以和sql92中的等值连接效果是一样的</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="6-2-1、等值查询"><a href="#6-2-1、等值查询" class="headerlink" title="6.2.1、等值查询"></a>6.2.1、等值查询</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"># <span class="number">1</span>、内连接</span><br><span class="line"><span class="comment">/*	语法：</span></span><br><span class="line"><span class="comment">SELECT 查询列表 FROM 表1 别名  INNER JOIN 表2 别名 ON 连接条件；</span></span><br><span class="line"><span class="comment">分类：等值、非等值、自连接</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># （<span class="number">1</span>）等值连接</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>）查询员工名，部门名</span><br><span class="line"><span class="keyword">SELECT</span> last_name,department_name <span class="keyword">FROM</span> employees e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> departments d <span class="keyword">ON</span> e.department_id<span class="operator">=</span>d.department_id;</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>）查询名字中包含e的员工名和工种名（添加筛选）</span><br><span class="line"><span class="keyword">SELECT</span> last_name,job_title <span class="keyword">FROM</span> employees e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> jobs j <span class="keyword">ON</span> e.job_id<span class="operator">=</span>j.job_id <span class="keyword">WHERE</span> e.last_name <span class="keyword">LIKE</span> <span class="string">&#x27;%e%&#x27;</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">3</span>）查询部门个数<span class="operator">&gt;</span><span class="number">3</span>的城市名和部门个数，（添加分组<span class="operator">+</span>筛选）</span><br><span class="line"># ①查询每个城市的部门个数</span><br><span class="line"># ②在①结果上筛选满足条件的</span><br><span class="line"><span class="keyword">SELECT</span> city,<span class="built_in">COUNT</span>(<span class="operator">*</span>) 部门个数 <span class="keyword">FROM</span> departments d <span class="keyword">INNER</span> <span class="keyword">JOIN</span> locations l <span class="keyword">ON</span> d.location_id<span class="operator">=</span>l.location_id <span class="keyword">GROUP</span> <span class="keyword">BY</span> city <span class="keyword">HAVING</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>)<span class="operator">&gt;</span><span class="number">3</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">4</span>）查询哪个部门的员工个数<span class="operator">&gt;</span><span class="number">3</span>的部门名和员工个数，并按个数降序（添加排序）</span><br><span class="line"># ①查询每个部门的员工个数</span><br><span class="line"># ②在①结果上筛选员工个数<span class="operator">&gt;</span><span class="number">3</span>的记录，并排序</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) , department_name <span class="keyword">FROM</span> employees e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> departments d <span class="keyword">ON</span> e.department_id<span class="operator">=</span>d.department_id <span class="keyword">GROUP</span> <span class="keyword">BY</span> department_name <span class="keyword">HAVING</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>)<span class="operator">&gt;</span><span class="number">3</span> <span class="keyword">ORDER</span> <span class="keyword">BY</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="keyword">DESC</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">5</span>）查询员工名、部门名、工种名，并按部门名降序</span><br><span class="line"><span class="keyword">SELECT</span> last_name,department_name,job_title <span class="keyword">FROM</span> employees e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> departments d <span class="keyword">ON</span> e.department_id<span class="operator">=</span>d.department_id <span class="keyword">INNER</span> <span class="keyword">JOIN</span> jobs j <span class="keyword">ON</span> e.job_id<span class="operator">=</span>j.job_id <span class="keyword">ORDER</span> <span class="keyword">BY</span> department_name <span class="keyword">DESC</span>;</span><br></pre></td></tr></table></figure>
<h4 id="6-2-2、非等值查询"><a href="#6-2-2、非等值查询" class="headerlink" title="6.2.2、非等值查询"></a>6.2.2、非等值查询</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 非等值查询</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、查询员工的工资级别</span><br><span class="line"><span class="keyword">SELECT</span> salary,grade_level <span class="keyword">FROM</span> employees e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job_grades j <span class="keyword">ON</span> e.salary <span class="keyword">BETWEEN</span> j.lowest_sal <span class="keyword">AND</span> j.highest_sal;</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、查询每个工资级别的个数<span class="operator">&gt;</span><span class="number">20</span>的个数，并且按工资级别降序</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>),salary,grade_level <span class="keyword">FROM</span> employees e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job_grades j <span class="keyword">ON</span> e.salary <span class="keyword">BETWEEN</span> j.lowest_sal <span class="keyword">AND</span> j.highest_sal <span class="keyword">GROUP</span> <span class="keyword">BY</span> grade_level <span class="keyword">HAVING</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>)<span class="operator">&gt;</span><span class="number">20</span> <span class="keyword">ORDER</span> <span class="keyword">BY</span> grade_level <span class="keyword">DESC</span>;</span><br></pre></td></tr></table></figure>
<h4 id="6-2-3、自连接"><a href="#6-2-3、自连接" class="headerlink" title="6.2.3、自连接"></a>6.2.3、自连接</h4><p><strong>和等值查询类似，自连接是自己连接自己，例如三级分类表</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 自连接</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、查询员工名和上级</span><br><span class="line"><span class="keyword">SELECT</span> e.last_name ,m.last_name <span class="keyword">FROM</span> employees e  <span class="keyword">INNER</span> <span class="keyword">JOIN</span> employees m <span class="keyword">ON</span> e.manager_id<span class="operator">=</span>m.employee_id <span class="keyword">WHERE</span> e.last_name <span class="keyword">LIKE</span> <span class="string">&#x27;%k%&#x27;</span>;</span><br></pre></td></tr></table></figure>
<h4 id="6-2-4、外连接"><a href="#6-2-4、外连接" class="headerlink" title="6.2.4、外连接"></a>6.2.4、外连接</h4><ul>
<li><strong>英文图示</strong></li>
</ul>
<p><img src= "" data-lazy-src="https://www.naste.top:9000/webp/sql连接.jfif" alt="image-20211029140510650"></p>
<ul>
<li><strong>中文图示</strong></li>
</ul>
<p><img src= "" data-lazy-src="https://www.naste.top:9000/webp/左右外连接.jfif" alt="image-20211029140510650"></p>
<p><strong>应用场景：用于查询一个表中有， 另一个表没有的记录</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"># 外连接</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">应用场景：用于查询一个表中有， 另一个表没有的记录</span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">	1、外连接的查询结果为主表中的所有记录【主要想要哪张表中的记录，哪张表即为主表】</span></span><br><span class="line"><span class="comment">		如果从表中有和它匹配的，则显示匹配的值</span></span><br><span class="line"><span class="comment">		如果从表中没有和它匹配的，则显示null</span></span><br><span class="line"><span class="comment">		外连接查询结果 = 内连接结果 + 主表中有而从表没有的记录</span></span><br><span class="line"><span class="comment">	2、左外连接，left左边的是主表</span></span><br><span class="line"><span class="comment">	   右外连接，right join 右边的是主表</span></span><br><span class="line"><span class="comment">	3、主外和右外交换两个表的顺序，可以实现同样的效果</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># 查询没有男朋友的女生</span><br><span class="line"></span><br><span class="line"># （<span class="number">1</span>）左外连接</span><br><span class="line"><span class="keyword">SELECT</span> b.name,bo.<span class="operator">*</span> <span class="keyword">FROM</span> beauty b <span class="keyword">LEFT</span> <span class="keyword">OUTER</span> <span class="keyword">JOIN</span> boys bo <span class="keyword">ON</span> b.boyfriend_id<span class="operator">=</span>bo.id <span class="keyword">WHERE</span> bo.id <span class="keyword">IS</span> <span class="keyword">NULL</span>;</span><br><span class="line"># （<span class="number">2</span>）右外连接</span><br><span class="line"><span class="keyword">SELECT</span> b.name,bo.<span class="operator">*</span> <span class="keyword">FROM</span> boys bo <span class="keyword">RIGHT</span> <span class="keyword">OUTER</span> <span class="keyword">JOIN</span> beauty b <span class="keyword">ON</span> b.boyfriend_id<span class="operator">=</span>bo.id <span class="keyword">WHERE</span> bo.id <span class="keyword">IS</span> <span class="keyword">NULL</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="6-2-5、全外连接"><a href="#6-2-5、全外连接" class="headerlink" title="6.2.5、全外连接"></a>6.2.5、全外连接</h4><p><strong>由于MySQL不支持全外连接，需要了结上网查询</strong></p>
<h4 id="6-2-6、交叉连接"><a href="#6-2-6、交叉连接" class="headerlink" title="6.2.6、交叉连接"></a>6.2.6、交叉连接</h4><p><strong>交叉连接就是使用sql99语法实现了笛卡尔乘积的效果</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 交叉连接——使用了sql99语法实现的笛卡尔乘积的效果</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> beauty b <span class="keyword">CROSS</span> <span class="keyword">JOIN</span> boys bo;</span><br></pre></td></tr></table></figure>
<h3 id="6-3、sql92和sql99总结"><a href="#6-3、sql92和sql99总结" class="headerlink" title="6.3、sql92和sql99总结"></a>6.3、sql92和sql99总结</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># sql92和sql99</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">功能：sql99支持的较多</span></span><br><span class="line"><span class="comment">可读性：sql99实现的连接条件和筛选条件的分离，可读性较高</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h2 id="7、子查询"><a href="#7、子查询" class="headerlink" title="7、子查询"></a>7、子查询</h2><h3 id="7-1、放在where后面"><a href="#7-1、放在where后面" class="headerlink" title="7.1、放在where后面"></a>7.1、放在where后面</h3><blockquote>
<p><strong>语法</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"># 子查询</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">含义：出现在其他语句中的SELECT语句，成为子查询或内查询。外部的查询语句，成为主查询或外查询</span></span><br><span class="line"><span class="comment">分类：</span></span><br><span class="line"><span class="comment">	按子查询出现的位置：</span></span><br><span class="line"><span class="comment">		SELECT后面：</span></span><br><span class="line"><span class="comment">			【标量子查询】</span></span><br><span class="line"><span class="comment">		FROM后面：</span></span><br><span class="line"><span class="comment">			【表子查询】</span></span><br><span class="line"><span class="comment">		WHERE或HAVING后面：⭐</span></span><br><span class="line"><span class="comment">			【标量子查询⭐、列子查询⭐、行子查询】</span></span><br><span class="line"><span class="comment">		EXISTS后面（相关子查询）：</span></span><br><span class="line"><span class="comment">			【表子查询】</span></span><br><span class="line"><span class="comment">	按结果集的行列数不同：</span></span><br><span class="line"><span class="comment">		标量子查询（结果集只有一行一列）</span></span><br><span class="line"><span class="comment">		列子查询（结果集只有一列多行）</span></span><br><span class="line"><span class="comment">		行子查询（结果集只有一行多列）</span></span><br><span class="line"><span class="comment">		表子查询（结果集一般为多行多列）</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>标量子查询</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"># <span class="number">1</span>、标量子查询</span><br><span class="line"></span><br><span class="line"># （<span class="number">1</span>）谁的工资比Abel高</span><br><span class="line"># ①查询Abel的工资</span><br><span class="line"><span class="keyword">SELECT</span> salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> last_name <span class="operator">=</span><span class="string">&#x27;Abel&#x27;</span>;</span><br><span class="line"># 查询员工的信息，满足salary<span class="operator">&gt;</span>①的结果</span><br><span class="line"><span class="keyword">SELECT</span> salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> salary <span class="operator">&gt;</span> (<span class="keyword">SELECT</span> salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> last_name <span class="operator">=</span><span class="string">&#x27;Abel&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># （<span class="number">2</span>）返回job_id与<span class="number">141</span>号员工相同，salary比<span class="number">143</span>号员工多的员工 姓名，job_id和工资</span><br><span class="line"># ①查询<span class="number">141</span>号员工的job_id</span><br><span class="line"><span class="keyword">SELECT</span> job_id <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> employee_id<span class="operator">=</span><span class="number">141</span>;</span><br><span class="line"># ②查询<span class="number">143</span>号员工的工资</span><br><span class="line"><span class="keyword">SELECT</span> salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> employee_id<span class="operator">=</span><span class="number">143</span>;</span><br><span class="line"># ③根据①和②的结果查询员工的姓名，job_id和工资</span><br><span class="line"><span class="keyword">SELECT</span> last_name,job_id,salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span></span><br><span class="line"> job_id<span class="operator">=</span>(<span class="keyword">SELECT</span> job_id <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> employee_id<span class="operator">=</span><span class="number">141</span>) <span class="keyword">AND</span> </span><br><span class="line"> salary<span class="operator">&gt;</span>(<span class="keyword">SELECT</span> salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> employee_id<span class="operator">=</span><span class="number">143</span>);</span><br><span class="line"> </span><br><span class="line"> # （<span class="number">3</span>）返回公司工资最少的员工的last_name,job_id和salary</span><br><span class="line"> </span><br><span class="line"> # ①查询最低工资</span><br><span class="line"> <span class="keyword">SELECT</span> <span class="built_in">MIN</span>(salary) <span class="keyword">FROM</span> employees;</span><br><span class="line"> # ②根据①的结果查询需要的员工信息</span><br><span class="line"> <span class="keyword">SELECT</span> last_name,job_id,salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> salary <span class="operator">=</span> (<span class="keyword">SELECT</span> <span class="built_in">MIN</span>(salary) <span class="keyword">FROM</span> employees);</span><br><span class="line"> </span><br><span class="line"> # （<span class="number">4</span>）查询最低工资大于<span class="number">50</span>号部门最低工资的部门id和其最低工资</span><br><span class="line"> # ①查询<span class="number">50</span>号部门的最低工资</span><br><span class="line"> <span class="keyword">SELECT</span> <span class="built_in">MIN</span>(salary) <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> department_id <span class="operator">=</span> <span class="number">50</span>;</span><br><span class="line"> # ②查询每个部门的最低工资</span><br><span class="line"> <span class="keyword">SELECT</span> <span class="built_in">MIN</span>(salary),department_id <span class="keyword">FROM</span> employees <span class="keyword">GROUP</span> <span class="keyword">BY</span> department_id;</span><br><span class="line"> # ③在②基础上，满足<span class="built_in">min</span>(salary)<span class="operator">&gt;</span>①</span><br><span class="line"> <span class="keyword">SELECT</span> <span class="built_in">MIN</span>(salary),department_id <span class="keyword">FROM</span> employees <span class="keyword">GROUP</span> <span class="keyword">BY</span> department_id <span class="keyword">HAVING</span> <span class="built_in">MIN</span>(salary)<span class="operator">&gt;</span>(<span class="keyword">SELECT</span> <span class="built_in">MIN</span>(salary) <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> department_id <span class="operator">=</span> <span class="number">50</span>);</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>列子查询</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"># 列子查询（多行子查询）【一列多行，比如id集合】</span><br><span class="line"></span><br><span class="line"># （<span class="number">1</span>）返回location_id是<span class="number">1400</span>或<span class="number">1700</span>的部门中的所有员工姓名</span><br><span class="line"># ①查询location_id是<span class="number">1400</span>或<span class="number">1700</span>的部门编号</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> department_id <span class="keyword">FROM</span> departments <span class="keyword">WHERE</span> location_id <span class="keyword">IN</span>(<span class="number">1400</span>,<span class="number">1700</span>);</span><br><span class="line"># ②查询员工姓名，要求部门是①列表中的某一个</span><br><span class="line"><span class="keyword">SELECT</span> last_name <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> department_id <span class="keyword">IN</span> (<span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> department_id <span class="keyword">FROM</span> departments <span class="keyword">WHERE</span> location_id <span class="keyword">IN</span>(<span class="number">1400</span>,<span class="number">1700</span>));</span><br><span class="line"></span><br><span class="line"># （<span class="number">3</span>）返回其他部门中比job_id为`IT_PROG`部门任一工资都低的员工的员工号、姓名、job_id以及salary</span><br><span class="line"># ①查询job_id为IT_PROG部门任一工资</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> job_id<span class="operator">=</span><span class="string">&#x27;IT_PROG&#x27;</span>;</span><br><span class="line"># ②查询员工工号、姓名、job_id以及salary，slary<span class="operator">&lt;</span>①的任意一个</span><br><span class="line"><span class="keyword">SELECT</span> last_name,employee_id,job_id,salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> salary<span class="operator">&lt;</span><span class="keyword">ANY</span>( <span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> job_id<span class="operator">=</span><span class="string">&#x27;IT_PROG&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># （<span class="number">3</span>）返回其他部门中比job_id为`IT_PROG`部门所有工资都低的员工的员工号、姓名、job_id以及salary</span><br><span class="line"> <span class="keyword">SELECT</span> last_name,employee_id,job_id,salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> salary<span class="operator">&lt;</span><span class="keyword">ALL</span>( <span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> salary <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> job_id<span class="operator">=</span><span class="string">&#x27;IT_PROG&#x27;</span>);</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>行子查询</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># <span class="number">3</span>、行子查询</span><br><span class="line"></span><br><span class="line"># （<span class="number">1</span>）查询员工编号最小并且工资最高的员工信息</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>）使用非行子查询</span><br><span class="line"># ①查询最小员工编号</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MIN</span>(employee_id) <span class="keyword">FROM</span> employees;</span><br><span class="line"># ②查询最高工资</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary) <span class="keyword">FROM</span> employees;</span><br><span class="line"># ③查询员工信息</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> employee_id<span class="operator">=</span>(<span class="keyword">SELECT</span> <span class="built_in">MIN</span>(employee_id) <span class="keyword">FROM</span> employees) <span class="keyword">AND</span> salary<span class="operator">=</span>(<span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary) <span class="keyword">FROM</span> employees);</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>）使用行子查询【使用不多】</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">WHERE</span>(employee_id,salary)<span class="operator">=</span>(<span class="keyword">SELECT</span> <span class="built_in">MIN</span>(employee_id),<span class="built_in">MAX</span>(salary) <span class="keyword">FROM</span> employees);</span><br></pre></td></tr></table></figure>
<h3 id="7-2、放在select后面"><a href="#7-2、放在select后面" class="headerlink" title="7.2、放在select后面"></a>7.2、放在select后面</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># <span class="keyword">SELECT</span>后面【仅仅支持标量子查询】</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、查询每个部门的员工个数</span><br><span class="line"><span class="keyword">SELECT</span> d.<span class="operator">*</span> , (<span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="keyword">FROM</span> employees e <span class="keyword">WHERE</span> d.department_id<span class="operator">=</span>e.department_id) <span class="keyword">FROM</span> departments d;</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、查询员工号<span class="operator">=</span><span class="number">102</span>的部门名</span><br><span class="line"><span class="keyword">SELECT</span> (<span class="keyword">SELECT</span> d.department_name <span class="keyword">FROM</span> departments d <span class="keyword">INNER</span> <span class="keyword">JOIN</span> employees e <span class="keyword">ON</span> d.department_id <span class="operator">=</span> e.department_id <span class="keyword">WHERE</span> e.employee_id<span class="operator">=</span><span class="number">102</span>) 部门名;</span><br></pre></td></tr></table></figure>
<h3 id="7-3、放在from后面"><a href="#7-3、放在from后面" class="headerlink" title="7.3、放在from后面"></a>7.3、放在from后面</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"># <span class="keyword">FROM</span>后面：<span class="keyword">FROM</span>后面查询出来的数据是一张虚拟表，需要给起别名</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">将子查询结果充当一张表， 要求必须起别名</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"># <span class="number">1</span>、查询每个部门的平均工资的工资等级</span><br><span class="line"># ①查询每个部门的平均工资</span><br><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	<span class="built_in">AVG</span>( salary ) avg,</span><br><span class="line">	department_id </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees </span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line">	department_id;</span><br><span class="line"># ②连接①的结果集和job_grades表，筛选条件平均工资 <span class="keyword">between</span> lowest_sal <span class="keyword">and</span> highest_sal</span><br><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	ag_dep.<span class="operator">*</span>,</span><br><span class="line">	g.grade_level </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	( <span class="keyword">SELECT</span> <span class="built_in">AVG</span>( salary ) avg, department_id <span class="keyword">FROM</span> employees <span class="keyword">GROUP</span> <span class="keyword">BY</span> department_id ) ag_dep</span><br><span class="line">	<span class="keyword">INNER</span> <span class="keyword">JOIN</span> job_grades g <span class="keyword">ON</span> ag_dep.avg <span class="keyword">BETWEEN</span> lowest_sal </span><br><span class="line">	<span class="keyword">AND</span> highest_sal;</span><br></pre></td></tr></table></figure>
<h3 id="7-4、exists后面（相关子查询）"><a href="#7-4、exists后面（相关子查询）" class="headerlink" title="7.4、exists后面（相关子查询）"></a>7.4、exists后面（相关子查询）</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># <span class="keyword">EXISTS</span>后面（相关子查询）</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">EXISTS（完整的查询语句）</span></span><br><span class="line"><span class="comment">结果：1/0</span></span><br><span class="line"><span class="comment">如果子查询有结果，返回1。如果子查询为null，返回0</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">EXISTS</span>(<span class="keyword">SELECT</span> employee_id <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> salary <span class="operator">=</span><span class="number">30000</span>) 是否存在;</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、查询有员工的部门名</span><br><span class="line"><span class="keyword">SELECT</span> department_name <span class="keyword">FROM</span> departments d <span class="keyword">WHERE</span> <span class="keyword">EXISTS</span>(<span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees e <span class="keyword">WHERE</span> d.department_id<span class="operator">=</span>e.department_id);</span><br></pre></td></tr></table></figure>
<h2 id="8、分页查询"><a href="#8、分页查询" class="headerlink" title="8、分页查询"></a>8、分页查询</h2><blockquote>
<p><strong>语法</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"># 分页查询</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">应用场景：当要显示的数据，一页显示补全，需要分页提交sql请求</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">	SELECT 查询列表 FROM 表 </span></span><br><span class="line"><span class="comment">	【</span></span><br><span class="line"><span class="comment">		JOIN TYPE JOIN 表2 </span></span><br><span class="line"><span class="comment">		ON 连接条件</span></span><br><span class="line"><span class="comment">		WHERE 筛选条件</span></span><br><span class="line"><span class="comment">		GROUP BY 分组字段</span></span><br><span class="line"><span class="comment">		HAVING 分组后的筛选</span></span><br><span class="line"><span class="comment">		ORDER BY 排序字段</span></span><br><span class="line"><span class="comment">	】</span></span><br><span class="line"><span class="comment">	LIMIT offset,size;</span></span><br><span class="line"><span class="comment">	offset：从第几条数据开始查询，0是第一条</span></span><br><span class="line"><span class="comment">	size：查询几条数据</span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">	①LIMIT语句放在查询语句的最后</span></span><br><span class="line"><span class="comment">	②公式：</span></span><br><span class="line"><span class="comment">		要显示的页数page，每页显示的条目数size</span></span><br><span class="line"><span class="comment">		LIMIT (page-1)*size , size</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>案例</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># <span class="number">1</span>、查询前五条员工信息</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees LIMIT <span class="number">0</span>,<span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、查询第<span class="number">11</span><span class="number">-25</span>条</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees LIMIT <span class="number">10</span>,<span class="number">15</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">3</span>、查询有奖金的员工信息，并且工资较高的前<span class="number">10</span>名显示出来</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> commission_pct <span class="keyword">IS</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">ORDER</span> <span class="keyword">BY</span> commission_pct <span class="keyword">DESC</span> LIMIT <span class="number">0</span>,<span class="number">10</span>;</span><br></pre></td></tr></table></figure>
<h2 id="9、union联合查询"><a href="#9、union联合查询" class="headerlink" title="9、union联合查询"></a>9、union联合查询</h2><blockquote>
<p><strong>语法</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"># 联合查询（<span class="keyword">union</span>）</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">union：联合、合并。将多条查询语句的结果合并成一个结果</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">	查询语句1</span></span><br><span class="line"><span class="comment">	UNION</span></span><br><span class="line"><span class="comment">	查询语句2</span></span><br><span class="line"><span class="comment">	UNION</span></span><br><span class="line"><span class="comment">	.......</span></span><br><span class="line"><span class="comment">应用场景：</span></span><br><span class="line"><span class="comment">	要查询的结果来自多个表，且多个表没有直接的连接关系，但查询的信息一致时</span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">	1、要求多条查询语句的列数是一致的</span></span><br><span class="line"><span class="comment">	2、要求多条查询语句的查询的每一列的类型和顺序最好一致</span></span><br><span class="line"><span class="comment">	3、UNION关键字默认去重，如果使用 UNION ALL可以包含重复项</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>案例</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"># <span class="number">1</span>、查询部门编号<span class="operator">&gt;</span><span class="number">90</span>或邮箱中包含a的员工信息</span><br><span class="line"></span><br><span class="line">#（<span class="number">1</span>）非联合查询</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> email <span class="keyword">LIKE</span> <span class="string">&#x27;%a%&#x27;</span> <span class="keyword">OR</span> department_id<span class="operator">&gt;</span><span class="number">90</span>;</span><br><span class="line">#（<span class="number">2</span>）联合查询——去重</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> email <span class="keyword">LIKE</span> <span class="string">&#x27;%a%&#x27;</span> </span><br><span class="line"><span class="keyword">UNION</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> department_id<span class="operator">&gt;</span><span class="number">90</span>;</span><br><span class="line">#（<span class="number">3</span>）联合查询——非去重（<span class="keyword">UNION</span>后面加上<span class="keyword">ALL</span>关键字）</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> email <span class="keyword">LIKE</span> <span class="string">&#x27;%a%&#x27;</span> </span><br><span class="line"><span class="keyword">UNION</span> <span class="keyword">ALL</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> department_id<span class="operator">&gt;</span><span class="number">90</span>;</span><br></pre></td></tr></table></figure>
<h1 id="五、DML（数据操纵语言）"><a href="#五、DML（数据操纵语言）" class="headerlink" title="五、DML（数据操纵语言）"></a>五、DML（数据操纵语言）</h1><blockquote>
<p><strong>插入语句</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"># DML语言</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">数据操纵语言：</span></span><br><span class="line"><span class="comment">	插入：INSERT</span></span><br><span class="line"><span class="comment">	修改：UPDATE</span></span><br><span class="line"><span class="comment">	删除：DELETE</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># 一、插入语句</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">	方式一：</span></span><br><span class="line"><span class="comment">		①INSERT INTO 表名(列名,......) VALUES(值1,......);</span></span><br><span class="line"><span class="comment">		②INSERT INTO 表名(列名,......) VALUE(值1,......);</span></span><br><span class="line"><span class="comment">		③INSERT INTO 表名(列名,......) </span></span><br><span class="line"><span class="comment">			VALUES(值1,......),</span></span><br><span class="line"><span class="comment">			VALUES(值1,......),</span></span><br><span class="line"><span class="comment">			VALUES(值1,......)</span></span><br><span class="line"><span class="comment">	方式二：</span></span><br><span class="line"><span class="comment">		INSERT INTO 表名 SET 列名=值,列名=值,......;</span></span><br><span class="line"><span class="comment">两种方式比较：</span></span><br><span class="line"><span class="comment">	（1）方式一可以插入多行，方式二不支持</span></span><br><span class="line"><span class="comment">	（2）方式一支持子查询，方式二不支持</span></span><br><span class="line"><span class="comment">	INSERT INTO 表(字段,字段,......) SELECT 字段,字段,...... FROM 表 条件</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、插入的值的类型要与列的类型一致或兼容</span><br><span class="line"># <span class="number">2</span>、不可以为<span class="keyword">null</span>的列必须插入值</span><br><span class="line"># <span class="number">3</span>、可以为<span class="keyword">null</span>的列，可以将值设置为<span class="keyword">null</span>。可以不写列名，列值，默认为<span class="keyword">null</span>。</span><br><span class="line"># <span class="number">4</span>、列的顺序可以调换</span><br><span class="line"># <span class="number">5</span>、列数和值的个数必须一致</span><br><span class="line"># <span class="number">6</span>、可以省略列名，默认所有列，而且顺序和表中列的顺序一致</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>修改语句</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"># 二、修改语句</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">1、修改单表记录⭐</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">	UPDATE 表名 SET 列名=数值,列名=数值,......【WHERE 筛选条件】</span></span><br><span class="line"><span class="comment">2、修改多表记录【补充】</span></span><br><span class="line"><span class="comment">语法【支持92和99语法】：</span></span><br><span class="line"><span class="comment">	（1）92语法</span></span><br><span class="line"><span class="comment">					UPDATE 表1 别名,表2 别名 SET 列=值,...... WHERE 连接条件 AND 筛选条件;</span></span><br><span class="line"><span class="comment">	（2）99语法</span></span><br><span class="line"><span class="comment">					UPDATE 表1 别名 INNER|LEFT|RIGHT JOIN 表2 别名 ON 连接条件 SET 列=值,...... WHERE 筛选条件</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>删除语句</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">方式一：DELETE</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">	1、单表的删除【⭐】</span></span><br><span class="line"><span class="comment">		语法：</span></span><br><span class="line"><span class="comment">			DELETE FROM 表名 WHERE 筛选条件</span></span><br><span class="line"><span class="comment">	2、多表删除【补充】:清空数据,将指定表中的数据全部删除,相当于【DELETE FROM TABLE】</span></span><br><span class="line"><span class="comment">		语法：</span></span><br><span class="line"><span class="comment">			TRUNCATE TABLE 表名;</span></span><br><span class="line"><span class="comment">DELETE和TRUNCATE比较：</span></span><br><span class="line"><span class="comment">	1、DELETE 可以加 WHERE条件，TRUNCATE不能加</span></span><br><span class="line"><span class="comment">	2、TRUNCATE删除，效率高一点</span></span><br><span class="line"><span class="comment">	3、假如要删除的表中有自增长列，使用delete删除后，再插入数据，自增长列的值从断点开始。而 TRUNCATE删除后，再插入数据，自增长列的值从1开始</span></span><br><span class="line"><span class="comment">	4、TRUNCATE没有返回值，DELETE有返回值</span></span><br><span class="line"><span class="comment">	5、TRUNCATE删除不能回滚，DELETE删除可以回滚</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h1 id="六、DDL（数据定义语言）"><a href="#六、DDL（数据定义语言）" class="headerlink" title="六、DDL（数据定义语言）"></a>六、DDL（数据定义语言）</h1><h2 id="1、库和表的操作"><a href="#1、库和表的操作" class="headerlink" title="1、库和表的操作"></a>1、库和表的操作</h2><blockquote>
<p><strong>定义</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">数据定义语言</span></span><br><span class="line"><span class="comment">库和表的管理</span></span><br><span class="line"><span class="comment">一、库的管理</span></span><br><span class="line"><span class="comment">	创建、修改、删除</span></span><br><span class="line"><span class="comment">二、表的管理</span></span><br><span class="line"><span class="comment">	创建、修改、删除</span></span><br><span class="line"><span class="comment">创建：CREATE</span></span><br><span class="line"><span class="comment">修改：ALTER</span></span><br><span class="line"><span class="comment">删除：DROP</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>库的操作</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"># 一、库的管理</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、库的创建</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">	CREATE DATABASE 库名;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">CREATE</span> DATABASE IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span> books_test;</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、库的修改【在新版本的MySQL中，该语句不能使用】</span><br><span class="line">RENAME DATABASE books_test <span class="keyword">TO</span> book_test;</span><br><span class="line"></span><br><span class="line"># <span class="number">3</span>、更改库的字符集</span><br><span class="line"><span class="keyword">ALTER</span> DATABASE books_test <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8mb4;</span><br><span class="line"></span><br><span class="line"># <span class="number">4</span>、库的删除</span><br><span class="line"><span class="keyword">DROP</span> DATABASE IF <span class="keyword">EXISTS</span> books_test;</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>表的操作</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"># 表的管理</span><br><span class="line"># <span class="number">1</span>、表的创建（⭐）</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">	CREATE TABLE 表名(</span></span><br><span class="line"><span class="comment">			列名 列的类型 【(长度) 约束】,</span></span><br><span class="line"><span class="comment">			列名 列的类型 【(长度) 约束】,</span></span><br><span class="line"><span class="comment">			列名 列的类型 【(长度) 约束】,</span></span><br><span class="line"><span class="comment">			......</span></span><br><span class="line"><span class="comment">	)</span></span><br><span class="line"><span class="comment">	</span></span><br><span class="line"><span class="comment">通用写法：先删除旧的，再建新的</span></span><br><span class="line"><span class="comment">	DROP DATABASE IF EXISTS 旧库名&#x27;</span></span><br><span class="line"><span class="comment">	CREATE DATABASE 新库名;</span></span><br><span class="line"><span class="comment">	DROP TABLE IF EXISTS 旧表名;</span></span><br><span class="line"><span class="comment">	CREATE TABLE 新表名(......);</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、创建book表</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span> `book` ( id <span class="type">INT</span>, #编号</span><br><span class="line">	book_name <span class="type">VARCHAR</span> ( <span class="number">20</span> ), #书名</span><br><span class="line">	price <span class="keyword">DOUBLE</span>, # 价格</span><br><span class="line">	authId <span class="type">VARCHAR</span> ( <span class="number">20</span> ), # 作者编号</span><br><span class="line">public_date DATETIME # 出版日期</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"># <span class="number">3</span>、查看表的信息</span><br><span class="line"><span class="keyword">DESC</span> `book`;</span><br><span class="line"><span class="keyword">DESC</span> `author`;</span><br><span class="line"></span><br><span class="line"># 创建表author</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `author` ( id <span class="type">INT</span>, #编号</span><br><span class="line">	auth_name <span class="type">VARCHAR</span> ( <span class="number">20</span> ), # 作者名字</span><br><span class="line">	nation <span class="type">VARCHAR</span> ( <span class="number">10</span> ) # 国籍</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"># <span class="number">4</span>、表的修改</span><br><span class="line"># ①修改列名</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> `book` CHANGE <span class="keyword">COLUMN</span> authId auth_id <span class="type">INT</span>;</span><br><span class="line"># ②修改列的类型或约束</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> `book` MODIFY <span class="keyword">COLUMN</span> price <span class="type">DECIMAL</span>;</span><br><span class="line"># ③添加新的列</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> author <span class="keyword">ADD</span> <span class="keyword">COLUMN</span> annual <span class="keyword">DOUBLE</span>;</span><br><span class="line"># ④删除列</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> author <span class="keyword">DROP</span> <span class="keyword">COLUMN</span> annual;</span><br><span class="line"># ⑤修改表名</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> author RENAME <span class="keyword">TO</span> book_author; </span><br><span class="line"></span><br><span class="line"># <span class="number">5</span>、表的删除</span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> book_author;</span><br><span class="line"><span class="keyword">SHOW</span> TABLES;</span><br><span class="line"></span><br><span class="line"># <span class="number">6</span>、表的复制</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> author <span class="keyword">VALUES</span>(<span class="number">1</span>,<span class="string">&#x27;村上春树&#x27;</span>,<span class="string">&#x27;日本&#x27;</span>),(<span class="number">2</span>,<span class="string">&#x27;莫言&#x27;</span>,<span class="string">&#x27;中国&#x27;</span>),(<span class="number">3</span>,<span class="string">&#x27;金庸&#x27;</span>,<span class="string">&#x27;中国&#x27;</span>);</span><br><span class="line"># （<span class="number">1</span>）只复制表全部结构</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="keyword">copy</span> <span class="keyword">LIKE</span> author;</span><br><span class="line"># （<span class="number">2</span>）复制表的结构和全部数据</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> copy2 <span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> author;</span><br><span class="line"># （<span class="number">3</span>）复制表的结构和部分数据</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> copy3 <span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> author <span class="keyword">WHERE</span> nation<span class="operator">=</span><span class="string">&#x27;中国&#x27;</span>;</span><br><span class="line"># （<span class="number">4</span>）复制表的部分字段，<span class="number">0</span>表示<span class="literal">false</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> copy4 <span class="keyword">SELECT</span> id,auth_name <span class="keyword">FROM</span> author <span class="keyword">WHERE</span> <span class="number">0</span>;</span><br></pre></td></tr></table></figure>
<h2 id="2、表的数据类型"><a href="#2、表的数据类型" class="headerlink" title="2、表的数据类型"></a>2、表的数据类型</h2><h3 id="2-1、整型"><a href="#2-1、整型" class="headerlink" title="2.1、整型"></a>2.1、整型</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"># 整型</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">TINYINT、SMALLINT、MEDIUMINT、INT/INTEGER、BIGINT</span></span><br><span class="line"><span class="comment">	1					2					3						4						5</span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">	1、如果不设置无符号还是有符号，默认是有符号。如果设置无符号，需要加UNSIGNED关键字。</span></span><br><span class="line"><span class="comment">	2、如果插入的数字超出了指定类型的范围，会报异常，且插入的值为指定类型的临界值</span></span><br><span class="line"><span class="comment">	3、如果不设置长度，会有默认的长度。长度显示的最大宽度，如果不够会用0在左边进行填充，但必须搭配 ZEROFILL 使用，如果加上了ZEROFILL之后，会默认为无符号。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"># 如何设置无符号和有符号</span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> tab_int;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> tab_int(</span><br><span class="line">	t1 <span class="type">INT</span> ,# 不加UNSIGNED关键字为有符号</span><br><span class="line">	t2 <span class="type">INT</span> UNSIGNED,# 加上UNSIGNED设置为无符号</span><br><span class="line">	t3 <span class="type">INT</span> ZEROFILL# 加上ZEROFILL后默认变为无符号</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h3 id="2-2、浮点型"><a href="#2-2、浮点型" class="headerlink" title="2.2、浮点型"></a>2.2、浮点型</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"># 浮点型</span><br><span class="line"></span><br><span class="line"># 小数</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">1、浮点型：</span></span><br><span class="line"><span class="comment">	FLOAT(M,D)、DOUBLE(M,D)</span></span><br><span class="line"><span class="comment">2、定点型</span></span><br><span class="line"><span class="comment">	DEC(M,D)、DECIMAL(M,D)</span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">	1、</span></span><br><span class="line"><span class="comment">		M：整数部位+小数部位</span></span><br><span class="line"><span class="comment">		D：小数部位</span></span><br><span class="line"><span class="comment">		如果超过范围，则插入临界值</span></span><br><span class="line"><span class="comment">	2、</span></span><br><span class="line"><span class="comment">		M和D可以省略</span></span><br><span class="line"><span class="comment">		如果是DECIMAL，则M默认为10，D默认为0</span></span><br><span class="line"><span class="comment">		如果是FLOAT和DOUBLE，则会根据插入数值的精度来决定精度</span></span><br><span class="line"><span class="comment">	3、</span></span><br><span class="line"><span class="comment">		定点型的精度较高，如果要求插入数值的进度较高，如货币运算等考虑使用</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">原则：</span></span><br><span class="line"><span class="comment">	所选择的类型越简单越好，能保存数值的类型越小越好</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、测试M和D</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> tab_float(</span><br><span class="line">		f1 <span class="type">FLOAT</span>(<span class="number">5</span>,<span class="number">2</span>),</span><br><span class="line">		f2 <span class="keyword">DOUBLE</span>(<span class="number">5</span>,<span class="number">2</span>),</span><br><span class="line">		f3 <span class="type">DECIMAL</span>(<span class="number">5</span>,<span class="number">2</span>)</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<h3 id="2-3、字符型"><a href="#2-3、字符型" class="headerlink" title="2.3、字符型"></a>2.3、字符型</h3><p><img src= "" data-lazy-src="https://www.naste.top:9000/webp/字符长度.jfif" alt="image-20211029140510650"></p>
<div class="table-container">
<table>
<thead>
<tr>
<th></th>
<th>写法</th>
<th>M的意思</th>
<th>特点</th>
<th>空间的耗费</th>
<th>效率</th>
</tr>
</thead>
<tbody>
<tr>
<td>char</td>
<td>char(M)</td>
<td>最大的字符数，可以省略，默认为1</td>
<td>固定长度的字符</td>
<td>比较耗费</td>
<td>高</td>
</tr>
<tr>
<td>varchar</td>
<td>varchar(M)</td>
<td>最大的字符数，不可以省略</td>
<td>可变长度的字符</td>
<td>比较节省</td>
<td>低</td>
</tr>
</tbody>
</table>
</div>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"># 字符型</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">较短的文本：</span></span><br><span class="line"><span class="comment">	CHAR、VARCHAR</span></span><br><span class="line"><span class="comment">	其他：</span></span><br><span class="line"><span class="comment">		BINARY和VARBINARY用于保存较短的二进制</span></span><br><span class="line"><span class="comment">		ENUM用于保存枚举</span></span><br><span class="line"><span class="comment">		SET用于保存集合</span></span><br><span class="line"><span class="comment">较长的文本：</span></span><br><span class="line"><span class="comment">	TEXT、BLOB（较大的二进制，例如图片）</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h3 id="2-4、日期类型"><a href="#2-4、日期类型" class="headerlink" title="2.4、日期类型"></a>2.4、日期类型</h3><div class="table-container">
<table>
<thead>
<tr>
<th></th>
<th>字节</th>
<th>范围</th>
<th>是否受时区等影响</th>
</tr>
</thead>
<tbody>
<tr>
<td>DATETIME</td>
<td>8</td>
<td>1000-9999</td>
<td>不受</td>
</tr>
<tr>
<td>TIMESTAMP</td>
<td>4</td>
<td>1970-2038</td>
<td>受</td>
</tr>
</tbody>
</table>
</div>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"># 日期类型</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">分类：</span></span><br><span class="line"><span class="comment">DATE：只保存日期</span></span><br><span class="line"><span class="comment">TIME：只保存时间</span></span><br><span class="line"><span class="comment">YEAR：只保存年</span></span><br><span class="line"><span class="comment">DATETIME：保存日期+时间</span></span><br><span class="line"><span class="comment">TIMESTAMP：保存日期+时间</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h2 id="3、常见约束"><a href="#3、常见约束" class="headerlink" title="3、常见约束"></a>3、常见约束</h2><h3 id="3-1、创建表时添加约束"><a href="#3-1、创建表时添加约束" class="headerlink" title="3.1、创建表时添加约束"></a>3.1、创建表时添加约束</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"># 常见约束</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">含义：一种限制，用于限制表中的数据，为了保证表中的数据的准确和可靠性</span></span><br><span class="line"><span class="comment">分类：六大约束</span></span><br><span class="line"><span class="comment">	NOT NULL：非空，用于保证该字段不能为空【比如姓名，学号等】</span></span><br><span class="line"><span class="comment">	DEFAULT：默认，用于保证该字段有默认值【比如性别】</span></span><br><span class="line"><span class="comment">	PRIMARY KEY：主键，用于保证该字段的值具有唯一性，并且具有非空【比如学号，员工编号等】</span></span><br><span class="line"><span class="comment">	UNIQUE：唯一，用于保证该字段的唯一性，可以为空【比如座位号】</span></span><br><span class="line"><span class="comment">	CHECK：检查约束【MySQL中不支持（SQL serve支持）。比如性别只能男和女，其他的旧不满足，比如用户的年龄范围为1-100，可以使用该约束条件】</span></span><br><span class="line"><span class="comment">	FOREIGN KEY：外键，用于限制两个表的关系，用于保证该字段的值必须来自主表的关联列的值，在从表添加外键约束，用于引用主表中某列的值，比如商品的类别编号，来自类别表中的编号</span></span><br><span class="line"><span class="comment">添加约束的时机：</span></span><br><span class="line"><span class="comment">	1、创建表时</span></span><br><span class="line"><span class="comment">	2、修改表时</span></span><br><span class="line"><span class="comment">约束的添加分类：</span></span><br><span class="line"><span class="comment">	列级约束：</span></span><br><span class="line"><span class="comment">		六大约束语法上都支持，但外键约束没有效果</span></span><br><span class="line"><span class="comment">	表级约束：</span></span><br><span class="line"><span class="comment">		除了非空、默认，其他都支持</span></span><br><span class="line"><span class="comment">外键：</span></span><br><span class="line"><span class="comment">	1、要求在从表设置外键关系</span></span><br><span class="line"><span class="comment">	2、从表的外键列的类型和主表的关联列的类型要求一致或兼容，名称无要求</span></span><br><span class="line"><span class="comment">	3、要求主表关联列必须是一个key(一般是主键或唯一)</span></span><br><span class="line"><span class="comment">	4、插入数据时，先插入主表，再插入从表</span></span><br><span class="line"><span class="comment">	5、删除数据时，先删除从表，再删除主表</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> 表名(</span><br><span class="line">		字段名 字段类型 列级约束 列级约束 ... ,</span><br><span class="line">		字段名 字段类型,</span><br><span class="line">		表级约束</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>主键和唯一的对比</strong></p>
</blockquote>
<div class="table-container">
<table>
<thead>
<tr>
<th></th>
<th>保证唯一性</th>
<th>是否允许为空</th>
<th>一个表中可以有多少个</th>
<th>是否允许组合【联合主键 】</th>
</tr>
</thead>
<tbody>
<tr>
<td>主键索引</td>
<td>√</td>
<td>×</td>
<td>至多有一个</td>
<td>√【不推荐】</td>
</tr>
<tr>
<td>唯一索引</td>
<td>√</td>
<td>√</td>
<td>可以有多个</td>
<td>√【不推荐】</td>
</tr>
</tbody>
</table>
</div>
<blockquote>
<p><strong>列级约束</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"># 一、创建表时添加的约束</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、添加列级约束</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">	直接在字段名和类型后面追加约束类型即可，只支持，默认，非空，主键，唯一</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> major ( </span><br><span class="line">	id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY, # 主键</span><br><span class="line">	majorName <span class="type">VARCHAR</span> ( <span class="number">20</span> ) </span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> stuinfo (</span><br><span class="line">	id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY,# 主键</span><br><span class="line">	stuName <span class="type">VARCHAR</span> ( <span class="number">20</span> ) <span class="keyword">NOT</span> <span class="keyword">NULL</span>,# 非空</span><br><span class="line">	gender <span class="type">CHAR</span> ( <span class="number">1</span> ) <span class="keyword">CHECK</span> ( gender <span class="operator">=</span> <span class="string">&#x27;男&#x27;</span> <span class="keyword">OR</span> gender <span class="operator">=</span> <span class="string">&#x27;女&#x27;</span> ),# 检查约束（MySQL不支持，但是可以写，没有效果）</span><br><span class="line">	seat <span class="type">INT</span> <span class="keyword">UNIQUE</span> ,# 唯一约束，座位号</span><br><span class="line">	age <span class="type">INT</span> <span class="keyword">DEFAULT</span> <span class="number">18</span>,# 默认</span><br><span class="line">	majorId <span class="type">INT</span> <span class="keyword">REFERENCES</span> major(id)# 外键</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"># 查询指定表的索引，包括主键，外键，唯一</span><br><span class="line"><span class="keyword">SHOW</span> INDEX <span class="keyword">FROM</span> stuinfo;</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>表级约束</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"># 添加表级约束</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">	在各个字段的最下面</span></span><br><span class="line"><span class="comment">		【CONSTRAINT 约束名】 约束类型(字段名)</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> major ( </span><br><span class="line">	id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY, # 主键</span><br><span class="line">	majorName <span class="type">VARCHAR</span> ( <span class="number">20</span> ) </span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> stuinfo;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> stuinfo (</span><br><span class="line">	id <span class="type">INT</span>,</span><br><span class="line">	stuname <span class="type">VARCHAR</span> ( <span class="number">20</span> ),</span><br><span class="line">	gender <span class="type">CHAR</span> ( <span class="number">1</span> ),</span><br><span class="line">	seat <span class="type">INT</span>,</span><br><span class="line">	age <span class="type">INT</span>,</span><br><span class="line">	majorId <span class="type">INT</span>,</span><br><span class="line">	# 添加表级约束</span><br><span class="line">	<span class="keyword">CONSTRAINT</span> pk <span class="keyword">PRIMARY</span> KEY ( id ),# 主键</span><br><span class="line">	<span class="keyword">CONSTRAINT</span> uq <span class="keyword">UNIQUE</span> ( seat ),# 唯一</span><br><span class="line">	<span class="keyword">CONSTRAINT</span> ck <span class="keyword">CHECK</span> ( gender <span class="operator">=</span> <span class="string">&#x27;男&#x27;</span> <span class="keyword">OR</span> gender <span class="operator">=</span> <span class="string">&#x27;女&#x27;</span> ),# 检查约束（MySQL不支持，但是可以写，没有效果） </span><br><span class="line">	<span class="keyword">CONSTRAINT</span> `fk_stu_major` <span class="keyword">FOREIGN</span> KEY(`majorId`) <span class="keyword">REFERENCES</span> `major`(`id`)# 外键</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"># 查询指定表的索引，包括主键，外键，唯一</span><br><span class="line"><span class="keyword">SHOW</span> INDEX <span class="keyword">FROM</span> stuinfo;</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>通用写法</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">#通用的写法：</span><br><span class="line"></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> major ( </span><br><span class="line">	id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY, # 主键</span><br><span class="line">	majorName <span class="type">VARCHAR</span> ( <span class="number">20</span> ) </span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"># 设置单个主键和唯一索引★</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span> stuinfo(</span><br><span class="line">	id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY,</span><br><span class="line">	stuname <span class="type">VARCHAR</span>(<span class="number">20</span>),</span><br><span class="line">	sex <span class="type">CHAR</span>(<span class="number">1</span>),</span><br><span class="line">	age <span class="type">INT</span> <span class="keyword">DEFAULT</span> <span class="number">18</span>,</span><br><span class="line">	seat <span class="type">INT</span> <span class="keyword">UNIQUE</span>,</span><br><span class="line">	majorid <span class="type">INT</span>,</span><br><span class="line">	<span class="keyword">CONSTRAINT</span> fk_stuinfo_major <span class="keyword">FOREIGN</span> KEY(majorid) <span class="keyword">REFERENCES</span> major(id)</span><br><span class="line"></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"># 设置联合组件和联合索引【不推荐使用】</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span> stuinfo(</span><br><span class="line">	id <span class="type">INT</span>,</span><br><span class="line">	stuname <span class="type">VARCHAR</span> ( <span class="number">20</span> ),</span><br><span class="line">	gender <span class="type">CHAR</span> ( <span class="number">1</span> ),</span><br><span class="line">	seat <span class="type">INT</span>,</span><br><span class="line">	age <span class="type">INT</span>,</span><br><span class="line">	majorId <span class="type">INT</span>,</span><br><span class="line">	# 添加表级约束</span><br><span class="line">	<span class="keyword">CONSTRAINT</span> pk <span class="keyword">PRIMARY</span> KEY ( id,stuname ),# 联合组件</span><br><span class="line">	<span class="keyword">CONSTRAINT</span> uq <span class="keyword">UNIQUE</span> ( seat,stuname ),# 联合唯一索引</span><br><span class="line">	<span class="keyword">CONSTRAINT</span> ck <span class="keyword">CHECK</span> ( gender <span class="operator">=</span> <span class="string">&#x27;男&#x27;</span> <span class="keyword">OR</span> gender <span class="operator">=</span> <span class="string">&#x27;女&#x27;</span> ),# 检查约束（MySQL不支持，但是可以写，没有效果） </span><br><span class="line">	<span class="keyword">CONSTRAINT</span> `fk_stu_major` <span class="keyword">FOREIGN</span> KEY(`majorId`) <span class="keyword">REFERENCES</span> `major`(`id`)# 外键</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h3 id="3-2、修改表时添加约束"><a href="#3-2、修改表时添加约束" class="headerlink" title="3.2、修改表时添加约束"></a>3.2、修改表时添加约束</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"># 二、修改表时添加约束</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">1、添加列级约束</span></span><br><span class="line"><span class="comment">ALTER TABLE 表名 MODIFY COLUMN 字段名 字段类型 新约束;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">2、添加表级约束</span></span><br><span class="line"><span class="comment">ALTER TABLE 表名 ADD 【CONSTRAINT 约束名】 约束类型(字段名) 【外键的引用】;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、添加非空约束 </span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> stuinfo MODIFY <span class="keyword">COLUMN</span> stuname <span class="type">VARCHAR</span>(<span class="number">20</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、添加默认约束</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> stuinfo MODIFY <span class="keyword">COLUMN</span> age <span class="type">INT</span> <span class="keyword">DEFAULT</span> <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">3</span>、添加主键</span><br><span class="line"># （<span class="number">1</span>）列级约束</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> stuinfo MODIFY <span class="keyword">COLUMN</span> id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY;</span><br><span class="line"># （<span class="number">2</span>）表级约束</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> stuinfo <span class="keyword">ADD</span> <span class="keyword">PRIMARY</span> KEY(id);</span><br><span class="line"></span><br><span class="line">#<span class="number">4.</span>添加唯一</span><br><span class="line"># （<span class="number">1</span>）列级约束</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> stuinfo MODIFY <span class="keyword">COLUMN</span> seat <span class="type">INT</span> <span class="keyword">UNIQUE</span>;</span><br><span class="line"># （<span class="number">2</span>）表级约束</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> stuinfo <span class="keyword">ADD</span> <span class="keyword">UNIQUE</span>(seat);</span><br><span class="line"></span><br><span class="line">#<span class="number">5.</span>添加外键</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> stuinfo <span class="keyword">ADD</span> <span class="keyword">CONSTRAINT</span> fk_stuinfo_major <span class="keyword">FOREIGN</span> KEY(majorid) <span class="keyword">REFERENCES</span> major(id); </span><br></pre></td></tr></table></figure>
<h3 id="3-3、修改表时删除约束"><a href="#3-3、修改表时删除约束" class="headerlink" title="3.3、修改表时删除约束"></a>3.3、修改表时删除约束</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">#<span class="number">1.</span>删除非空约束</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> stuinfo MODIFY <span class="keyword">COLUMN</span> stuname <span class="type">VARCHAR</span>(<span class="number">20</span>) <span class="keyword">NULL</span>;</span><br><span class="line"></span><br><span class="line">#<span class="number">2.</span>删除默认约束</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> stuinfo MODIFY <span class="keyword">COLUMN</span> age <span class="type">INT</span> ;</span><br><span class="line"></span><br><span class="line">#<span class="number">3.</span>删除主键</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> stuinfo <span class="keyword">DROP</span> <span class="keyword">PRIMARY</span> KEY;</span><br><span class="line"></span><br><span class="line">#<span class="number">4.</span>删除唯一</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> stuinfo <span class="keyword">DROP</span> INDEX seat;</span><br><span class="line"></span><br><span class="line">#<span class="number">5.</span>删除外键</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> stuinfo <span class="keyword">DROP</span> <span class="keyword">FOREIGN</span> KEY fk_stuinfo_major;</span><br></pre></td></tr></table></figure>
<h2 id="4、标识列"><a href="#4、标识列" class="headerlink" title="4、标识列"></a>4、标识列</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"># 标识列【AUTO_INCREMENT】</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">又称为自增长列</span></span><br><span class="line"><span class="comment">含义：可以不用手动的插入值，系统提供默认的序列值</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">	1、标识列必须和主键搭配吗？不一定，但要求是一个key</span></span><br><span class="line"><span class="comment">	2、一个表可以有几个标识列？至多一个！</span></span><br><span class="line"><span class="comment">	3、标识列的类型只能是数值型</span></span><br><span class="line"><span class="comment">	4、标识列可以通过 SET auto_increment_increment=3;设置步长</span></span><br><span class="line"><span class="comment">	5、可以通过 手动插入值，设置起始值</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line">#一、创建表时设置标识列</span><br><span class="line"> </span><br><span class="line"># 创建表，添加标识</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> tab_identity(</span><br><span class="line">	id <span class="type">INT</span>  ,</span><br><span class="line">	NAME <span class="type">FLOAT</span> <span class="keyword">UNIQUE</span> AUTO_INCREMENT</span><br><span class="line"></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"># 清空表</span><br><span class="line"><span class="keyword">TRUNCATE</span> <span class="keyword">TABLE</span> tab_identity; </span><br><span class="line"></span><br><span class="line"># 查看步长值</span><br><span class="line"><span class="keyword">SHOW</span> VARIABLES <span class="keyword">LIKE</span> <span class="string">&#x27;%auto_increment%&#x27;</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 设置步长值【步长值：每次加多少】一般不该</span><br><span class="line"><span class="keyword">SET</span> auto_increment_increment<span class="operator">=</span><span class="number">3</span>;</span><br></pre></td></tr></table></figure>
<h1 id="七、TCL语言（数据控制语言）"><a href="#七、TCL语言（数据控制语言）" class="headerlink" title="七、TCL语言（数据控制语言）"></a>七、TCL语言（数据控制语言）</h1><h2 id="1、事务"><a href="#1、事务" class="headerlink" title="1、事务"></a>1、事务</h2><ul>
<li>原子性：一个事务不可再分割，要么执行要么不执行</li>
<li>一致性：一个事务执行会使数据从一个一致状态切换到另一个一致状态</li>
<li>隔离性：一个事务的执行不受其他事务的干扰</li>
<li>持久性：一个事务一旦提交，则会永久的改变数据库的数据</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"># TCL</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">Transaction Control Language 事务控制语言</span></span><br><span class="line"><span class="comment">事务：</span></span><br><span class="line"><span class="comment">	一个或一组sql语句组成一个执行单元，这个执行单元要么全部执行，要么全部不执行。</span></span><br><span class="line"><span class="comment">事务的属性(ACID)：</span></span><br><span class="line"><span class="comment">	原子性：一个事务不可再分割，要么执行要么不执行</span></span><br><span class="line"><span class="comment">	一致性：一个事务执行会使数据从一个一致状态切换到另一个一致状态</span></span><br><span class="line"><span class="comment">	隔离性：一个事务的执行不受其他事务的干扰</span></span><br><span class="line"><span class="comment">	持久性：一个事务一旦提交，则会永久的改变数据库的数据</span></span><br><span class="line"><span class="comment">事务的创建</span></span><br><span class="line"><span class="comment">	隐式事务，事务没有明显的开启和结束的标记</span></span><br><span class="line"><span class="comment">		比如单条的insert、update、delete语句</span></span><br><span class="line"><span class="comment">	显式事务：事务具有明显的开启和结束标记</span></span><br><span class="line"><span class="comment">		前提：必须先设置自动提交功能为禁用</span></span><br><span class="line"><span class="comment">	设置事务是否自动提交</span></span><br><span class="line"><span class="comment">		SET autocommit=0;</span></span><br><span class="line"><span class="comment">事务开启步骤：</span></span><br><span class="line"><span class="comment">	1、步骤一：</span></span><br><span class="line"><span class="comment">		SET autocommit=0;</span></span><br><span class="line"><span class="comment">		START TRANSACTION;可选的</span></span><br><span class="line"><span class="comment">	2、步骤二：</span></span><br><span class="line"><span class="comment">		编写事务中的sql语句（SELECT INSERT UPDATE DELETE）</span></span><br><span class="line"><span class="comment">	3、步骤三：</span></span><br><span class="line"><span class="comment">		commit; 提交事务</span></span><br><span class="line"><span class="comment">		rollback; 回滚事务</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># 查看MySQL支持的存储引擎</span><br><span class="line"><span class="keyword">SHOW</span> ENGINES;</span><br><span class="line"></span><br><span class="line"># 查看事务自动提交状态</span><br><span class="line"><span class="keyword">SHOW</span> VARIABLES <span class="keyword">LIKE</span> <span class="string">&#x27;autocommit&#x27;</span></span><br><span class="line"></span><br><span class="line"># 示<span class="keyword">savepoint</span> 的使用</span><br><span class="line"><span class="keyword">SET</span> autocommit<span class="operator">=</span><span class="number">0</span>;</span><br><span class="line"><span class="keyword">START</span> TRANSACTION;</span><br><span class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> account <span class="keyword">WHERE</span> id<span class="operator">=</span><span class="number">25</span>;</span><br><span class="line"><span class="keyword">SAVEPOINT</span> a;#设置保存点</span><br><span class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> account <span class="keyword">WHERE</span> id<span class="operator">=</span><span class="number">28</span>;</span><br><span class="line"><span class="keyword">ROLLBACK</span> <span class="keyword">TO</span> a;#回滚到保存点</span><br><span class="line"></span><br><span class="line"># 演示事务的使用步骤</span><br><span class="line"></span><br><span class="line"># （<span class="number">1</span>）开启事务</span><br><span class="line"><span class="keyword">SET</span> autocommit<span class="operator">=</span><span class="number">0</span>;</span><br><span class="line"><span class="keyword">START</span> TRANSACTION;#可选的</span><br><span class="line"></span><br><span class="line"># （<span class="number">2</span>）编写<span class="keyword">sql</span>语句</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> admin(username,`password`) <span class="keyword">VALUES</span>(<span class="string">&#x27;123123&#x27;</span>,<span class="string">&#x27;123&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># （<span class="number">3</span>）结束事务，提交和回滚选择一个</span><br><span class="line"><span class="keyword">COMMIT</span>;# 提交</span><br><span class="line"><span class="keyword">ROLLBACK</span>;# 回滚</span><br></pre></td></tr></table></figure>
<h2 id="2、delete和truncate在事务使用时的区别"><a href="#2、delete和truncate在事务使用时的区别" class="headerlink" title="2、delete和truncate在事务使用时的区别"></a>2、delete和truncate在事务使用时的区别</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"># <span class="keyword">delete</span>和<span class="keyword">truncate</span>在事务使用时的区别</span><br><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span><span class="keyword">FROM</span> admin;</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、<span class="keyword">delete</span>：可以回滚</span><br><span class="line"><span class="keyword">SET</span> autocommit<span class="operator">=</span><span class="number">0</span>;# 关闭自动提交</span><br><span class="line"><span class="keyword">START</span> TRANSACTION; # 开启事务</span><br><span class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> admin;# 删除表中的所有数据</span><br><span class="line"><span class="keyword">ROLLBACK</span>;# 回滚，之前的操作无效</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、<span class="keyword">truncate</span>：不可以回滚</span><br><span class="line"><span class="keyword">SET</span> autocommit<span class="operator">=</span><span class="number">0</span>;# 关闭自动提交</span><br><span class="line"><span class="keyword">START</span> TRANSACTION; # 开启事务</span><br><span class="line"><span class="keyword">TRUNCATE</span> <span class="keyword">TABLE</span> admin;# 清空表</span><br><span class="line"><span class="keyword">ROLLBACK</span>;# 回滚</span><br></pre></td></tr></table></figure>
<h2 id="3、事务隔离级别"><a href="#3、事务隔离级别" class="headerlink" title="3、事务隔离级别"></a>3、事务隔离级别</h2><p><strong>具体看JDBC笔记</strong></p>
<h1 id="八、视图的讲解"><a href="#八、视图的讲解" class="headerlink" title="八、视图的讲解"></a>八、视图的讲解</h1><h2 id="1、视图的增删改查"><a href="#1、视图的增删改查" class="headerlink" title="1、视图的增删改查"></a>1、视图的增删改查</h2><blockquote>
<p><strong>视图的好处</strong></p>
</blockquote>
<ul>
<li>重用sql</li>
<li>简化复杂的sql操作，不必知道它的查询细节</li>
<li>保护数据，提高安全性</li>
</ul>
<blockquote>
<p><strong>视图创建和使用</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"># 视图</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">含义：一张虚拟的表，和普通表一样使用</span></span><br><span class="line"><span class="comment">mysql5.1版本出现新特性，是通过表动态生成的数据</span></span><br><span class="line"><span class="comment">比如</span></span><br><span class="line"><span class="comment">*/</span># 一、创建视图</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法：CREATE VIEW 视图名 AS 查询语句;</span></span><br><span class="line"><span class="comment">*/</span># <span class="number">1</span>、查询邮箱中包含a字符的员工名、部门名和工种信息</span><br><span class="line"></span><br><span class="line"># ①创建视图</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">VIEW</span> view1 <span class="keyword">AS</span> <span class="keyword">SELECT</span></span><br><span class="line">last_name,</span><br><span class="line">department_name,</span><br><span class="line">job_title </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees e</span><br><span class="line">	<span class="keyword">JOIN</span> departments d <span class="keyword">ON</span> e.department_id <span class="operator">=</span> d.department_id</span><br><span class="line">	<span class="keyword">JOIN</span> jobs j <span class="keyword">ON</span> j.job_id <span class="operator">=</span> e.job_id;</span><br><span class="line">	</span><br><span class="line"># ②使用视图</span><br><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	<span class="operator">*</span> </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	view1 </span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	last_name <span class="keyword">LIKE</span> <span class="string">&#x27;%a%&#x27;</span>;</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>视图修改</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"># 视图的修改</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">	方式一：</span></span><br><span class="line"><span class="comment">        CREATE OR REPLACE VIEW 视图名</span></span><br><span class="line"><span class="comment">        AS</span></span><br><span class="line"><span class="comment">        查询语句</span></span><br><span class="line"><span class="comment">	方式二：</span></span><br><span class="line"><span class="comment">		ALTER VIEW 视图名</span></span><br><span class="line"><span class="comment">		AS</span></span><br><span class="line"><span class="comment">		查询语句;</span></span><br><span class="line"><span class="comment">       </span></span><br><span class="line"><span class="comment">*/</span># <span class="number">1</span>、创建视图</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">VIEW</span> view1 <span class="keyword">AS</span> <span class="keyword">SELECT</span></span><br><span class="line">last_name,</span><br><span class="line">department_name,</span><br><span class="line">job_title </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees e</span><br><span class="line">	<span class="keyword">JOIN</span> departments d <span class="keyword">ON</span> e.department_id <span class="operator">=</span> d.department_id</span><br><span class="line">	<span class="keyword">JOIN</span> jobs j <span class="keyword">ON</span> j.job_id <span class="operator">=</span> e.job_id;</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、查询视图</span><br><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	<span class="operator">*</span> </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	view1;</span><br><span class="line">	</span><br><span class="line"># <span class="number">3.1</span>、方式一：修改视图</span><br><span class="line"><span class="keyword">CREATE</span> </span><br><span class="line">	<span class="keyword">OR</span> REPLACE <span class="keyword">VIEW</span> view1 <span class="keyword">AS</span> <span class="keyword">SELECT</span></span><br><span class="line">	<span class="operator">*</span> </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees e </span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	employee_id <span class="operator">&lt;</span> <span class="number">130</span>;</span><br><span class="line">	</span><br><span class="line"># <span class="number">3.2</span>、方式二：修改视图</span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">VIEW</span> view1 <span class="keyword">AS</span> <span class="keyword">SELECT</span></span><br><span class="line"><span class="operator">*</span> </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees e </span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	employee_id <span class="operator">&lt;</span> <span class="number">110</span>;</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>删除视图</strong></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 删除视图</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">	DROP VIEW 视图名，视图名,......;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">VIEW</span> view1;</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>查看视图</strong></p>
</blockquote>
<p><strong>查看视图的创建语句和视图的结构</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 查看视图</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、查看视图结构</span><br><span class="line"><span class="keyword">DESC</span> view1;</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、查看视图创建形式</span><br><span class="line"><span class="keyword">SHOW</span> <span class="keyword">CREATE</span> <span class="keyword">VIEW</span> view1;</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>视图更新</strong></p>
</blockquote>
<p><strong>视图中数据的增删改，视图中的增删改会影响原始表中的数据。视图中的数据在进行增删改的操作时可能会执行失败</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"># 一、视图更新</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、创建视图</span><br><span class="line"><span class="keyword">CREATE</span> </span><br><span class="line">	<span class="keyword">OR</span> REPLACE <span class="keyword">VIEW</span> myv1 <span class="keyword">AS</span> <span class="keyword">SELECT</span></span><br><span class="line">	last_name,</span><br><span class="line">	email </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees;</span><br><span class="line"></span><br><span class="line"># <span class="number">1</span>、插入</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> myv1 <span class="keyword">VALUES</span>(<span class="string">&#x27;张飞&#x27;</span>,<span class="string">&#x27;zf@qq.com&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># <span class="number">2</span>、修改</span><br><span class="line"><span class="keyword">UPDATE</span> myv1 <span class="keyword">SET</span> last_name <span class="operator">=</span> <span class="string">&#x27;张无忌&#x27;</span> <span class="keyword">WHERE</span> last_name<span class="operator">=</span><span class="string">&#x27;张飞&#x27;</span>;</span><br><span class="line"></span><br><span class="line"># <span class="number">3</span>、删除</span><br><span class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> myv1 <span class="keyword">WHERE</span> last_name <span class="operator">=</span> <span class="string">&#x27;张无忌&#x27;</span>;</span><br><span class="line"></span><br><span class="line"># 二、具备以下特点的视图不允许更新</span><br><span class="line"># <span class="number">1</span>、包含以下关键字的<span class="keyword">sql</span>语句：分组函数、<span class="keyword">distinct</span>、<span class="keyword">group</span>  <span class="keyword">by</span>、<span class="keyword">having</span>、<span class="keyword">union</span>或者<span class="keyword">union</span> <span class="keyword">all</span></span><br><span class="line"># <span class="number">2</span>、常量视图</span><br><span class="line"># <span class="number">3</span>、<span class="keyword">SELECT</span>中包含子查询</span><br><span class="line"># <span class="number">4</span>、<span class="keyword">JOIN</span>、多表连接</span><br><span class="line"># <span class="number">5</span>、<span class="keyword">FROM</span>一个不能更新的视图</span><br><span class="line"># <span class="number">6</span>、<span class="keyword">WHERE</span>子句的子查询引用了<span class="keyword">FROM</span>子句中的表</span><br></pre></td></tr></table></figure>
<h2 id="2、视图和表的对比"><a href="#2、视图和表的对比" class="headerlink" title="2、视图和表的对比"></a>2、视图和表的对比</h2><blockquote>
<p><strong>视图和表的对比</strong></p>
</blockquote>
<div class="table-container">
<table>
<thead>
<tr>
<th></th>
<th>创建语法的关键字</th>
<th>是否实际占用物理空间</th>
<th>使用</th>
</tr>
</thead>
<tbody>
<tr>
<td>视图</td>
<td>create view</td>
<td>只是保存了sql逻辑</td>
<td>增删改查。一般不能增删改查</td>
</tr>
<tr>
<td>表</td>
<td>create table</td>
<td>保存了数据</td>
<td>增删改查</td>
</tr>
</tbody>
</table>
</div>
<ul>
<li>包含以下关键字的sql语句：<ul>
<li>分组函数、distinct、group  by、having、union或者union all</li>
<li>常量视图</li>
<li>SELECT中包含子查询</li>
<li>JOIN、多表连接</li>
<li>FROM一个不能更新的视图</li>
<li>WHERE子句的子查询引用了FROM子句中的表</li>
</ul>
</li>
</ul>
<p>==<strong>后面的后续学习</strong>==</p>
<h1 id="九、变量"><a href="#九、变量" class="headerlink" title="九、变量"></a>九、变量</h1><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">#变量</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">系统变量：</span></span><br><span class="line"><span class="comment">	全局变量</span></span><br><span class="line"><span class="comment">	会话变量</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">自定义变量：</span></span><br><span class="line"><span class="comment">	用户变量</span></span><br><span class="line"><span class="comment">	局部变量</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#一、系统变量</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">说明：变量由系统定义，不是用户定义，属于服务器层面</span></span><br><span class="line"><span class="comment">注意：全局变量需要添加global关键字，会话变量需要添加session关键字，如果不写，默认会话级别</span></span><br><span class="line"><span class="comment">使用步骤：</span></span><br><span class="line"><span class="comment">	1、查看所有系统变量</span></span><br><span class="line"><span class="comment">		show global|【session】variables;</span></span><br><span class="line"><span class="comment">	2、查看满足条件的部分系统变量</span></span><br><span class="line"><span class="comment">		show global|【session】 variables like &#x27;%char%&#x27;;</span></span><br><span class="line"><span class="comment">	3、查看指定的系统变量的值</span></span><br><span class="line"><span class="comment">		select @@global|【session】系统变量名;</span></span><br><span class="line"><span class="comment">	4、为某个系统变量赋值</span></span><br><span class="line"><span class="comment">		方式一：</span></span><br><span class="line"><span class="comment">			set global|【session】系统变量名=值;</span></span><br><span class="line"><span class="comment">		方式二：</span></span><br><span class="line"><span class="comment">			set @@global|【session】系统变量名=值;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h1 id="十、存储过程和函数"><a href="#十、存储过程和函数" class="headerlink" title="十、存储过程和函数"></a>十、存储过程和函数</h1><h1 id="十一、流程控制结构"><a href="#十一、流程控制结构" class="headerlink" title="十一、流程控制结构"></a>十一、流程控制结构</h1></article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>MySQL基本使用</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="https://www.naste.top:1024/posts/75c6e52f.html">https://www.naste.top:1024/posts/75c6e52f.html</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>XiaoFei🥝</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2022-07-03</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2024-04-26</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/MySQL/"><div class="tags-punctuation"><svg class="faa-tada icon" style="height:1.1em;width:1.1em;fill:currentColor;position:relative;top:2px;margin-right:3px" aria-hidden="true"><use xlink:href="#icon-sekuaibiaoqian"></use></svg></div>MySQL</a></div></div><link rel="stylesheet" href="/css/coin.css" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">投喂作者</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://www.naste.top:9000/images/wechat.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="https://www.naste.top:9000/images/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://www.naste.top:9000/images/alipay.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="https://www.naste.top:9000/images/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><audio id="coinAudio" src="https://npm.elemecdn.com/akilar-candyassets@1.0.36/audio/aowu.m4a"></audio><script defer="defer" src="/js/coin.js"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/1406167115.html"><img class="prev-cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/1515952743765.webp" onerror="onerror=null;src='/assets/r2.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Linux</div></div></a></div><div class="next-post pull-right"><a href="/posts/f8e09374.html"><img class="next-cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/1515952743802.webp" onerror="onerror=null;src='/assets/r2.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Vue</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/2414116852.html" title="JDBC"><img class="cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/1515952743784.webp" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-04-26</div><div class="title">JDBC</div></div></a></div><div><a href="/posts/3169224211.html" title="MySQL基础"><img class="cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/1675912182.webp" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-04-26</div><div class="title">MySQL基础</div></div></a></div><div><a href="/posts/316116237.html" title="MySQL数据库备份"><img class="cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/1675912176.webp" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-04-26</div><div class="title">MySQL数据库备份</div></div></a></div><div><a href="/posts/994286255.html" title="Mysql的表无法修改、删除等操作，卡死、锁死情况的处理办法"><img class="cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/1678672925.webp" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-04-26</div><div class="title">Mysql的表无法修改、删除等操作，卡死、锁死情况的处理办法</div></div></a></div><div><a href="/posts/2844609012.html" title="第零-MySQL写在前面"><img class="cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/1675912181.webp" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-04-26</div><div class="title">第零-MySQL写在前面</div></div></a></div><div><a href="/posts/3474155355.html" title="第一-MySQL数据库概述"><img class="cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/1675912189.webp" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-04-26</div><div class="title">第一-MySQL数据库概述</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><svg class="meta_icon" style="width:22px;height:22px;position:relative;top:5px"><use xlink:href="#icon-mulu1"></use></svg><span style="font-weight:bold">目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E8%BF%9E%E6%8E%A5MySQL"><span class="toc-text">一、连接MySQL</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E3%80%81MySQL%E5%B8%B8%E8%A7%81%E5%91%BD%E4%BB%A4"><span class="toc-text">二、MySQL常见命令</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89%E3%80%81MySQL%E8%AF%AD%E6%B3%95%E8%A7%84%E8%8C%83"><span class="toc-text">三、MySQL语法规范</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9B%9B%E3%80%81DQL%EF%BC%88%E6%95%B0%E6%8D%AE%E6%9F%A5%E8%AF%A2%E8%AF%AD%E8%A8%80DQL%EF%BC%89"><span class="toc-text">四、DQL（数据查询语言DQL）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E5%9F%BA%E7%A1%80%E6%9F%A5%E8%AF%A2"><span class="toc-text">1、基础查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E6%9D%A1%E4%BB%B6%E6%9F%A5%E8%AF%A2"><span class="toc-text">2、条件查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E6%8E%92%E5%BA%8F%E6%9F%A5%E8%AF%A2"><span class="toc-text">3、排序查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E5%8D%95%E8%A1%8C%E5%87%BD%E6%95%B0"><span class="toc-text">4、单行函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1%E3%80%81%E5%B8%B8%E8%A7%81%E5%87%BD%E6%95%B0"><span class="toc-text">4.1、常见函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2%E3%80%81%E5%8D%95%E8%A1%8C%E5%87%BD%E6%95%B0%E6%80%BB%E7%BB%93"><span class="toc-text">4.2、单行函数总结</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E5%88%86%E7%BB%84%E5%87%BD%E6%95%B0"><span class="toc-text">5、分组函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1%E3%80%81%E5%B8%B8%E8%A7%81%E5%87%BD%E6%95%B0"><span class="toc-text">5.1、常见函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-2%E3%80%81%E5%88%86%E7%BB%84%E6%9F%A5%E8%AF%A2"><span class="toc-text">5.2、分组查询</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6%E3%80%81%E8%BF%9E%E6%8E%A5%E6%9F%A5%E8%AF%A2"><span class="toc-text">6、连接查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#6-1%E3%80%81sql92"><span class="toc-text">6.1、sql92</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#6-1-1%E3%80%81%E7%AD%89%E5%80%BC%E6%9F%A5%E8%AF%A2"><span class="toc-text">6.1.1、等值查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-1-2%E3%80%81%E9%9D%9E%E7%AD%89%E5%80%BC%E6%9F%A5%E8%AF%A2"><span class="toc-text">6.1.2、非等值查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-1-3%E3%80%81%E8%87%AA%E8%BF%9E%E6%8E%A5"><span class="toc-text">6.1.3、自连接</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-2%E3%80%81sql99"><span class="toc-text">6.2、sql99</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#6-2-1%E3%80%81%E7%AD%89%E5%80%BC%E6%9F%A5%E8%AF%A2"><span class="toc-text">6.2.1、等值查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-2-2%E3%80%81%E9%9D%9E%E7%AD%89%E5%80%BC%E6%9F%A5%E8%AF%A2"><span class="toc-text">6.2.2、非等值查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-2-3%E3%80%81%E8%87%AA%E8%BF%9E%E6%8E%A5"><span class="toc-text">6.2.3、自连接</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-2-4%E3%80%81%E5%A4%96%E8%BF%9E%E6%8E%A5"><span class="toc-text">6.2.4、外连接</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-2-5%E3%80%81%E5%85%A8%E5%A4%96%E8%BF%9E%E6%8E%A5"><span class="toc-text">6.2.5、全外连接</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-2-6%E3%80%81%E4%BA%A4%E5%8F%89%E8%BF%9E%E6%8E%A5"><span class="toc-text">6.2.6、交叉连接</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-3%E3%80%81sql92%E5%92%8Csql99%E6%80%BB%E7%BB%93"><span class="toc-text">6.3、sql92和sql99总结</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7%E3%80%81%E5%AD%90%E6%9F%A5%E8%AF%A2"><span class="toc-text">7、子查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#7-1%E3%80%81%E6%94%BE%E5%9C%A8where%E5%90%8E%E9%9D%A2"><span class="toc-text">7.1、放在where后面</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-2%E3%80%81%E6%94%BE%E5%9C%A8select%E5%90%8E%E9%9D%A2"><span class="toc-text">7.2、放在select后面</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-3%E3%80%81%E6%94%BE%E5%9C%A8from%E5%90%8E%E9%9D%A2"><span class="toc-text">7.3、放在from后面</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-4%E3%80%81exists%E5%90%8E%E9%9D%A2%EF%BC%88%E7%9B%B8%E5%85%B3%E5%AD%90%E6%9F%A5%E8%AF%A2%EF%BC%89"><span class="toc-text">7.4、exists后面（相关子查询）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2"><span class="toc-text">8、分页查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9%E3%80%81union%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="toc-text">9、union联合查询</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%94%E3%80%81DML%EF%BC%88%E6%95%B0%E6%8D%AE%E6%93%8D%E7%BA%B5%E8%AF%AD%E8%A8%80%EF%BC%89"><span class="toc-text">五、DML（数据操纵语言）</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AD%E3%80%81DDL%EF%BC%88%E6%95%B0%E6%8D%AE%E5%AE%9A%E4%B9%89%E8%AF%AD%E8%A8%80%EF%BC%89"><span class="toc-text">六、DDL（数据定义语言）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E5%BA%93%E5%92%8C%E8%A1%A8%E7%9A%84%E6%93%8D%E4%BD%9C"><span class="toc-text">1、库和表的操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E8%A1%A8%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-text">2、表的数据类型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1%E3%80%81%E6%95%B4%E5%9E%8B"><span class="toc-text">2.1、整型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2%E3%80%81%E6%B5%AE%E7%82%B9%E5%9E%8B"><span class="toc-text">2.2、浮点型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3%E3%80%81%E5%AD%97%E7%AC%A6%E5%9E%8B"><span class="toc-text">2.3、字符型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4%E3%80%81%E6%97%A5%E6%9C%9F%E7%B1%BB%E5%9E%8B"><span class="toc-text">2.4、日期类型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E5%B8%B8%E8%A7%81%E7%BA%A6%E6%9D%9F"><span class="toc-text">3、常见约束</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1%E3%80%81%E5%88%9B%E5%BB%BA%E8%A1%A8%E6%97%B6%E6%B7%BB%E5%8A%A0%E7%BA%A6%E6%9D%9F"><span class="toc-text">3.1、创建表时添加约束</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2%E3%80%81%E4%BF%AE%E6%94%B9%E8%A1%A8%E6%97%B6%E6%B7%BB%E5%8A%A0%E7%BA%A6%E6%9D%9F"><span class="toc-text">3.2、修改表时添加约束</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3%E3%80%81%E4%BF%AE%E6%94%B9%E8%A1%A8%E6%97%B6%E5%88%A0%E9%99%A4%E7%BA%A6%E6%9D%9F"><span class="toc-text">3.3、修改表时删除约束</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E6%A0%87%E8%AF%86%E5%88%97"><span class="toc-text">4、标识列</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%83%E3%80%81TCL%E8%AF%AD%E8%A8%80%EF%BC%88%E6%95%B0%E6%8D%AE%E6%8E%A7%E5%88%B6%E8%AF%AD%E8%A8%80%EF%BC%89"><span class="toc-text">七、TCL语言（数据控制语言）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E4%BA%8B%E5%8A%A1"><span class="toc-text">1、事务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81delete%E5%92%8Ctruncate%E5%9C%A8%E4%BA%8B%E5%8A%A1%E4%BD%BF%E7%94%A8%E6%97%B6%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">2、delete和truncate在事务使用时的区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E4%BA%8B%E5%8A%A1%E9%9A%94%E7%A6%BB%E7%BA%A7%E5%88%AB"><span class="toc-text">3、事务隔离级别</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AB%E3%80%81%E8%A7%86%E5%9B%BE%E7%9A%84%E8%AE%B2%E8%A7%A3"><span class="toc-text">八、视图的讲解</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E8%A7%86%E5%9B%BE%E7%9A%84%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5"><span class="toc-text">1、视图的增删改查</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E8%A7%86%E5%9B%BE%E5%92%8C%E8%A1%A8%E7%9A%84%E5%AF%B9%E6%AF%94"><span class="toc-text">2、视图和表的对比</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B9%9D%E3%80%81%E5%8F%98%E9%87%8F"><span class="toc-text">九、变量</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E3%80%81%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B%E5%92%8C%E5%87%BD%E6%95%B0"><span class="toc-text">十、存储过程和函数</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E4%B8%80%E3%80%81%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E7%BB%93%E6%9E%84"><span class="toc-text">十一、流程控制结构</span></a></li></ol></div></div></div></div></main><footer id="footer" style="background-color: transparent;"><div id="footer-wrap"><div id="ft"><div class="ft-item-1"><div class="t-top"><div class="t-t-l"><p class="ft-t t-l-t">格言🧬</p><div class="bg-ad"><div>再看看那个光点，它就在这里，这是家园，这是我们 —— 你所爱的每一个人，你认识的一个人，你听说过的每一个人，曾经有过的每一个人，都在它上面度过他们的一生✨</div><div class="btn-xz-box"><a class="btn-xz" target="_blank" rel="noopener" href="https://stellarium.org/">点击开启星辰之旅</a></div></div></div><div class="t-t-r"><p class="ft-t t-l-t">猜你想看💡</p><ul class="ft-links"><li><a href="/posts/eec9786.html">魔改指南</a><a href="/box/nav/">网址导航</a></li><li><a href="/social/link/">我的朋友</a><a href="/comments/">留点什么</a></li><li><a href="/personal/about/">关于作者</a><a href="/archives/">文章归档</a></li><li><a href="/categories/">文章分类</a><a href="/tags/">文章标签</a></li><li><a href="/box/Gallery/">我的画廊</a><a href="/personal/bb/">我的唠叨</a></li><li><a href="/site/time/">建设进程</a><a href="/site/census/">网站统计</a></li></ul></div></div></div><div class="ft-item-2"><p class="ft-t">推荐友链⌛</p><div class="ft-img-group"><div class="img-group-item"><a href="https://www.naste.top:1024/" title="XiaoFei🥝"><img src= "" data-lazy-src="https://www.naste.top:9000/images/cat.jpg" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div></div></div></div><div class="copyright"><span><b>&copy;2022-2024</b></span><span><b>&nbsp;&nbsp;By XiaoFei🥝</b></span></div><div id="workboard"></div><p id="ghbdages"><a class="github-badge" target="_blank" href="https://hexo.io/" style="margin-inline:5px" title="博客框架为Hexo_v6.3.0"><img src= "" data-lazy-src="https://static.naste.top:1024/svg/blog/Frame-Hexo-blue.svg" alt=""/></a><a class="github-badge" target="_blank" href="https://butterfly.js.org/" style="margin-inline:5px" title="主题版本Butterfly_v4.3.1"><img src= "" data-lazy-src="https://static.naste.top:1024/svg/blog/Theme-Butterfly-6513df.svg" alt=""/></a><a class="github-badge" target="_blank" href="https://vercel.com/" style="margin-inline:5px" title="本站采用多线部署，主线路托管于Vercel"><img src= "" data-lazy-src="https://static.naste.top:1024/svg/blog/Hosted-Vercel-brightgreen.svg" alt=""/></a><a class="github-badge" target="_blank" href="https://user.51.la/" style="margin-inline:5px" title="本站数据分析得益于51la技术支持"><img src= "" data-lazy-src="https://static.naste.top:1024/svg/blog/Analytics-51la-3db1eb.svg" alt=""/></a><a class="github-badge" target="_blank" href="https://github.com/" style="margin-inline:5px" title="本网站源码由Github提供存储仓库"><img src= "" data-lazy-src=" https://static.naste.top:1024/svg/blog/Source-Github-d021d6.svg" alt=""/></a></p></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><a class="icon-V hidden" onclick="switchNightMode()" title="浅色和深色模式转换"><svg width="25" height="25" viewBox="0 0 1024 1024"><use id="modeicon" xlink:href="#icon-moon"></use></svg></a><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button><button class="share" type="button" title="右键模式" onclick="changeMouseMode()"><i class="fas fa-mouse"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog right_side"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button class="share" type="button" title="分享链接" onclick="share()"><i class="fas fa-share-nodes"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i><span id="percent">0<span>%</span></span></button><button id="go-down" type="button" title="直达底部" onclick="btf.scrollToDest(document.body.scrollHeight, 500)"><i class="fas fa-arrow-down"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div class="js-pjax" id="rightMenu"><div class="rightMenu-group rightMenu-small"><a class="rightMenu-item" href="javascript:window.history.back();"><i class="fa fa-arrow-left"></i></a><a class="rightMenu-item" href="javascript:window.history.forward();"><i class="fa fa-arrow-right"></i></a><a class="rightMenu-item" href="javascript:window.location.reload();"><i class="fa fa-refresh"></i></a><a class="rightMenu-item" href="javascript:rmf.scrollToTop();"><i class="fa fa-arrow-up"></i></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-text"><a class="rightMenu-item" href="javascript:rmf.copySelect();"><i class="fa fa-copy"></i><span>复制</span></a><a class="rightMenu-item" href="javascript:window.open(&quot;https://www.baidu.com/s?wd=&quot;+window.getSelection().toString());window.location.reload();"><i class="fa fa-search"></i><span>百度搜索</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-too"><a class="rightMenu-item" href="javascript:window.open(window.getSelection().toString());window.location.reload();"><i class="fa fa-link"></i><span>转到链接</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-paste"><a class="rightMenu-item" href="javascript:rmf.paste()"><i class="fa fa-copy"></i><span>粘贴</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-post"><a class="rightMenu-item" href="#post-comment"><i class="fas fa-comment"></i><span>空降评论</span></a><a class="rightMenu-item" href="javascript:rmf.copyWordsLink()"><i class="fa fa-link"></i><span>复制本文地址</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-to"><a class="rightMenu-item" href="javascript:rmf.openWithNewTab()"><i class="fa fa-window-restore"></i><span>新窗口打开</span></a><a class="rightMenu-item" id="menu-too" href="javascript:rmf.open()"><i class="fa fa-link"></i><span>转到链接</span></a><a class="rightMenu-item" href="javascript:rmf.copyLink()"><i class="fa fa-copy"></i><span>复制链接</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-img"><a class="rightMenu-item" href="javascript:rmf.saveAs()"><i class="fa fa-download"></i><span>保存图片</span></a><a class="rightMenu-item" href="javascript:rmf.openWithNewTab()"><i class="fa fa-window-restore"></i><span>在新窗口打开</span></a><a class="rightMenu-item" href="javascript:rmf.copyLink()"><i class="fa fa-copy"></i><span>复制图片链接</span></a></div><div class="rightMenu-group rightMenu-line"><a class="rightMenu-item" href="javascript:randomPost()"><i class="fa fa-paper-plane"></i><span>随便逛逛</span></a><a class="rightMenu-item" href="javascript:switchNightMode();"><i class="fa fa-moon"></i><span>昼夜切换</span></a><a class="rightMenu-item" href="/personal/about/"><i class="fa fa-info-circle"></i><span>关于博客</span></a><a class="rightMenu-item" href="javascript:toggleWinbox();"><i class="fas fa-cog"></i><span>美化设置</span></a><a class="rightMenu-item" href="javascript:rmf.fullScreen();"><i class="fas fa-expand"></i><span>切换全屏</span></a><a class="rightMenu-item" href="javascript:window.print();"><i class="fa-solid fa-print"></i><span>打印页面</span></a></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.staticfile.org/fancyapps-ui/4.0.31/fancybox.umd.min.js"></script><script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/instant.page/5.1.0/instantpage.min.js" type="module"></script><script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vanilla-lazyload/17.3.1/lazyload.iife.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdnjs.cloudflare.com/ajax/libs/pangu/4.0.7/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (true){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/js/search/local-search.js"></script><script async="async">var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())
setTimeout(function(){preloader.endLoading();}, 5000);
document.getElementById('loading-box').addEventListener('click',()=> {preloader.endLoading()})</script><div class="js-pjax"><script>(()=>{
  const init = () => {
    twikoo.init(Object.assign({
      el: '#twikoo-wrap',
      envId: '',
      region: '',
      onCommentLoaded: function () {
        btf.loadLightbox(document.querySelectorAll('#twikoo .tk-content img:not(.tk-owo-emotion)'))
      }
    }, null))
  }

  const getCount = () => {
    const countELement = document.getElementById('twikoo-count')
    if(!countELement) return
    twikoo.getCommentsCount({
      envId: '',
      region: '',
      urls: [window.location.pathname],
      includeReply: false
    }).then(function (res) {
      countELement.innerText = res[0].count
    }).catch(function (err) {
      console.error(err);
    });
  }

  const runFn = () => {
    init()
    
  }

  const loadTwikoo = () => {
    if (typeof twikoo === 'object') {
      setTimeout(runFn,0)
      return
    } 
    getScript('https://cdn.staticfile.org/twikoo/1.6.8/twikoo.all.min.js').then(runFn)
  }

  if ('Twikoo' === 'Twikoo' || !true) {
    if (true) btf.loadComment(document.getElementById('twikoo-wrap'), loadTwikoo)
    else loadTwikoo()
  } else {
    window.loadOtherComment = () => {
      loadTwikoo()
    }
  }
})()</script></div><script src="https://cdn.staticfile.org/jquery/3.6.3/jquery.min.js"></script><script async src="https://cdn1.tianli0.top/npm/vue@2.6.14/dist/vue.min.js"></script><script async src="https://cdn1.tianli0.top/npm/element-ui@2.15.6/lib/index.js"></script><script async src="https://cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script><script defer type="text/javascript" src="https://cdn1.tianli0.top/npm/sweetalert2@8.19.0/dist/sweetalert2.all.js"></script><script async src="//npm.elemecdn.com/pace-js@1.2.4/pace.min.js"></script><script defer src="https://cdn1.tianli0.top/gh/nextapps-de/winbox/dist/winbox.bundle.min.js"></script><script async src="//at.alicdn.com/t/c/font_3586335_hsivh70x0fm.js"></script><script async src="//at.alicdn.com/t/c/font_3636804_gr02jmjr3y9.js"></script><script async src="//at.alicdn.com/t/c/font_3612150_kfv55xn3u2g.js"></script><script async src="https://cdn.wpon.cn/2022-sucai/Gold-ingot.js"></script><canvas id="universe"></canvas><canvas id="snow"></canvas><script defer src="/js/fomal.js"></script><link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/aplayer/1.10.1/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/aplayer/1.10.1/APlayer.min.js"></script><script src="https://cdn1.tianli0.top/npm/js-heo@1.0.12/metingjs/Meting.min.js"></script><script src="https://lib.baomitu.com/pjax/0.2.8/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show","#web_bg",".js-pjax","#bibi","body > title","#app","#tag-echarts","#posts-echart","#categories-echarts"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --> <script data-pjax>if(document.getElementById('recent-posts') && (location.pathname ==='/'|| '/' ==='all')){
    var parent = document.getElementById('recent-posts');
    var child = '<div class="recent-post-item" style="width:100%;height: auto"><div id="catalog_magnet"><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/Java/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍡 XiaoFeiのJava学习笔记 (55)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/Linux/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍼 XiaoFeiのLinux笔记 (33)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/Python/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍉 XiaoFeiのPython笔记 (2)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/前端/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍟 XiaoFeiの前端笔记 (17)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/Other/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍥 XiaoFeiのOther笔记 (10)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item" style="visibility: hidden"></div><a class="magnet_link_more"  href="https://www.naste.top:1024/categories/" style="flex:1;text-align: center;margin-bottom: 10px;">查看更多...</a></div></div>';
    console.log('已挂载magnet')
    parent.insertAdjacentHTML("afterbegin",child)}
     </script><style>#catalog_magnet{flex-wrap: wrap;display: flex;width:100%;justify-content:space-between;padding: 10px 10px 0 10px;align-content: flex-start;}.magnet_item{flex-basis: calc(33.333333333333336% - 5px);background: #e9e9e9;margin-bottom: 10px;border-radius: 8px;transition: all 0.2s ease-in-out;}.magnet_item:hover{background: var(--text-bg-hover)}.magnet_link_more{color:#555}.magnet_link{color:black}.magnet_link:hover{color:white}@media screen and (max-width: 600px) {.magnet_item {flex-basis: 100%;}}.magnet_link_context{display:flex;padding: 10px;font-size:16px;transition: all 0.2s ease-in-out;}.magnet_link_context:hover{padding: 10px 20px;}</style>
    <style></style><script data-pjax>
  function butterfly_swiper_injector_config(){
    var parent_div_git = document.getElementById('recent-posts');
    var item_html = '<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;posts/204253054.html&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src= "" data-lazy-src="https://www.naste.top:9000/webp/1675912191.webp" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-03-08</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;posts/204253054.html&quot;);" href="javascript:void(0);" alt="">SpringBoot使用实现定时任务和xxl-job实现分布式任务调度</a><div class="blog-slider__text">SpringBoot使用实现定时任务和xxl-job实现分布式任务调度</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;posts/204253054.html&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;posts/538133837.html&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src= "" data-lazy-src="https://www.naste.top:9000/webp/1678672933.webp" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-03-15</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;posts/538133837.html&quot;);" href="javascript:void(0);" alt="">Hexo中Markdown语法与外挂标签写法汇总</a><div class="blog-slider__text">Hexo中Markdown语法与外挂标签写法汇总</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;posts/538133837.html&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
    console.log('已挂载butterfly_swiper')
    parent_div_git.insertAdjacentHTML("afterbegin",item_html)
    }
  var elist = 'undefined'.split(',');
  var cpage = location.pathname;
  var epage = '/';
  var flag = 0;

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_swiper_injector_config();
  }
  else if (epage === cpage){
    butterfly_swiper_injector_config();
  }
  </script><script defer src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.js"></script><script defer data-pjax src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper_init.js"></script><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '2s');
    arr[i].setAttribute('data-wow-delay', '200ms');
    arr[i].setAttribute('data-wow-offset', '30');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '2s');
    arr[i].setAttribute('data-wow-delay', '200ms');
    arr[i].setAttribute('data-wow-offset', '30');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script></div><script defer src="https://npm.elemecdn.com/hexo-butterfly-wowjs/lib/wow.min.js"></script><script defer src="https://npm.elemecdn.com/hexo-butterfly-wowjs/lib/wow_init.js"></script><script data-pjax src="https://npm.elemecdn.com/hexo-filter-gitcalendar/lib/gitcalendar.js"></script><script data-pjax>
  function gitcalendar_injector_config(){
      var parent_div_git = document.getElementById('gitZone');
      var item_html = '<div class="recent-post-item" id="gitcalendarBar" style="width:100%;height:auto;padding:10px;"><style>#git_container{min-height: 320px}@media screen and (max-width:650px) {#git_container{min-height: 0px}}</style><div id="git_loading" style="width:10%;height:100%;margin:0 auto;display: block;"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 50 50" style="enable-background:new 0 0 50 50" xml:space="preserve"><path fill="#d0d0d0" d="M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z" transform="rotate(275.098 25 25)"><animatetransform attributeType="xml" attributeName="transform" type="rotate" from="0 25 25" to="360 25 25" dur="0.6s" repeatCount="indefinite"></animatetransform></path></svg><style>#git_container{display: none;}</style></div><div id="git_container"></div></div>';
      parent_div_git.insertAdjacentHTML("afterbegin",item_html)
      console.log('已挂载gitcalendar')
      }

    if( document.getElementById('gitZone') && (location.pathname ==='/personal/about/'|| '/personal/about/' ==='all')){
        gitcalendar_injector_config()
        GitCalendarInit("https://gitcalendar.naste.top/api?xiao-i-fei",['#d9e0df', '#c6e0dc', '#a8dcd4', '#9adcd2', '#89ded1', '#77e0d0', '#5fdecb', '#47dcc6', '#39dcc3', '#1fdabe', '#00dab9'],'xiao-i-fei')
    }
  </script><!-- hexo injector body_end end --></body></html>